ruby on rails - Insert value corresponding to foreign key in View (executing the application)? -
i'm beginner in rails. while i'm reading documentation , tutorials porting simple application have in production in language (java generated genexus)
i'm building employee shift tracker, have 2 tables: users , shifts, these associations
class user < activerecord::base has_many :shifts end class shift < activerecord::base belongs_to :user end
i added corresponding user_id in shift model when creating sqlite database.
my problem that, when trying insert new record via new view, "user_id" field doesn't validate correctly valid user_id number try write. i'm supposed insert in field? (in current running application in java field accept user_id value )
when tryng insert value inside "user" field in "shifts" view, following error
user(#63152640) expected, got string(#19315740)
here's view source
new.html.erb
<h1>new shift</h1> <%= render 'form' %> <%= link_to 'back', shifts_path %>
_form.html.erb
<%= form_for(@shift) |f| %> <% if @shift.errors.any? %> <div id="error_explanation"> <h2><%= pluralize(@shift.errors.count, "error") %> prohibited shift being saved:</h2> <ul> <% @shift.errors.full_messages.each |msg| %> <li><%= msg %></li> <% end %> </ul> </div> <% end %> <div class="field"> <%= f.label :start %><br /> <%= f.datetime_select :start %> </div> <div class="field"> <%= f.label :end %><br /> <%= f.datetime_select :end %> </div> <div class="field"> <%= f.label :status %><br /> <%= f.text_field :status %> </div> <div class="field"> <%= f.label :user %><br /> <%= f.text_field :user %> //<-- should write here? user_id? </div> <div class="actions"> <%= f.submit %> </div> <% end %>
(all of generated scaffolding) also, new action in shifts_controller is
def new @shift = shift.new end
i've figured out.
turns out view generated scaffolding, in user field this:
<div class="field"> <%= f.label :user %><br /> <%= f.text_field :user %> //<-- should write here? user_id? </div>
i changed :user
:user_id
, worked. tested writing 1 in user_id field , saved correctly.
then in index.html.erb, changed shift.user
in list shift.user.username
, list shows me shift belongs user correctly
Comments
Post a Comment