Rails properly pass ID through loop
up vote
0
down vote
favorite
Attempting to pass id's from my rails front end to back end. I have the following bit of code in my Rails controller:
def count_vote
roster_id = params[:roster_id]
roster = Roster.find(roster_id)
newvote = roster.vote + 1
if roster.update(vote: newvote)
redirect_to rosters_path
end
end
My rails view for roster has the following code:
<% @rosters.each do |roster| %>
<div class='each'>
<%= image_tag(roster['image_url'], class: 'image') %>
<%= hidden_field_tag(:roster_id, @roster.id) %>
<p class='name'> <%= roster['name'] %> </p>
<p class='title'> <%= roster['title'] %> </p>
<p> <%= roster['bio'] %> </p>
<p> <b> Want to work with <%= roster['name'] %>? </b> <%= link_to image_tag('thumbs-up.svg', class: 'thumbsup'), rosters_path, method: :patch %>
<br>
<%= roster['vote'] %> People have said Yes! </p>
<br>
</div>
<% end %>
I'm trying to pass the ID using the hidden field tag but I get the following error in my rails server logs:
Started PATCH "/rosters" for 127.0.0.1 at 2018-11-09 17:43:56 -0500
(0.2ms) SELECT "schema_migrations"."version" FROM "schema_migrations" ORDER BY "schema_migrations"."version" ASC
↳ /Users/sohel/.rbenv/versions/2.5.1/lib/ruby/gems/2.5.0/gems/activerecord-5.2.1/lib/active_record/log_subscriber.rb:98
Processing by RostersController#count_vote as HTML
Parameters: "authenticity_token"=>"DJAtI8yNTlP1kQK/g/6o9jbdqxWEBBgKinzLtf8v8WrDnYCmrH+HQI5wTEjJ0T6rkKbBz0KA/q2M0yirluozQg=="
Completed 404 Not Found in 14ms (ActiveRecord: 0.6ms)
ActiveRecord::RecordNotFound (Couldn't find Roster without an ID):
app/controllers/rosters_controller.rb:22:in `count_vote'
Searching Roster.all in my error console returns the following, for brevity sake I will return only the first two:
=> #<ActiveRecord::Relation [#<Roster id: 1, name: "John Smith", image_url: "https://d2eyrv63e6x6lp.cloudfront.net/wp-content/u...", title: "CTO / Co-founder", bio: "John has been programming since his dad bought him...", vote: 3, created_at: "2018-11-09 02:46:39", updated_at: "2018-11-09 02:46:39">, #<Roster id: 2, name: "Michael Thomas", image_url: "https://d2eyrv63e6x6lp.cloudfront.net/wp-content/u...", title: "Senior Software Engineer", bio: "Michael has been working as a back-end developer sin...", vote: 3, created_at: "2018-11-09 02:46:39", updated_at: "2018-11-09 02:46:39">
ruby-on-rails ruby
add a comment |
up vote
0
down vote
favorite
Attempting to pass id's from my rails front end to back end. I have the following bit of code in my Rails controller:
def count_vote
roster_id = params[:roster_id]
roster = Roster.find(roster_id)
newvote = roster.vote + 1
if roster.update(vote: newvote)
redirect_to rosters_path
end
end
My rails view for roster has the following code:
<% @rosters.each do |roster| %>
<div class='each'>
<%= image_tag(roster['image_url'], class: 'image') %>
<%= hidden_field_tag(:roster_id, @roster.id) %>
<p class='name'> <%= roster['name'] %> </p>
<p class='title'> <%= roster['title'] %> </p>
<p> <%= roster['bio'] %> </p>
<p> <b> Want to work with <%= roster['name'] %>? </b> <%= link_to image_tag('thumbs-up.svg', class: 'thumbsup'), rosters_path, method: :patch %>
<br>
<%= roster['vote'] %> People have said Yes! </p>
<br>
</div>
<% end %>
I'm trying to pass the ID using the hidden field tag but I get the following error in my rails server logs:
Started PATCH "/rosters" for 127.0.0.1 at 2018-11-09 17:43:56 -0500
(0.2ms) SELECT "schema_migrations"."version" FROM "schema_migrations" ORDER BY "schema_migrations"."version" ASC
↳ /Users/sohel/.rbenv/versions/2.5.1/lib/ruby/gems/2.5.0/gems/activerecord-5.2.1/lib/active_record/log_subscriber.rb:98
Processing by RostersController#count_vote as HTML
Parameters: "authenticity_token"=>"DJAtI8yNTlP1kQK/g/6o9jbdqxWEBBgKinzLtf8v8WrDnYCmrH+HQI5wTEjJ0T6rkKbBz0KA/q2M0yirluozQg=="
Completed 404 Not Found in 14ms (ActiveRecord: 0.6ms)
ActiveRecord::RecordNotFound (Couldn't find Roster without an ID):
app/controllers/rosters_controller.rb:22:in `count_vote'
Searching Roster.all in my error console returns the following, for brevity sake I will return only the first two:
=> #<ActiveRecord::Relation [#<Roster id: 1, name: "John Smith", image_url: "https://d2eyrv63e6x6lp.cloudfront.net/wp-content/u...", title: "CTO / Co-founder", bio: "John has been programming since his dad bought him...", vote: 3, created_at: "2018-11-09 02:46:39", updated_at: "2018-11-09 02:46:39">, #<Roster id: 2, name: "Michael Thomas", image_url: "https://d2eyrv63e6x6lp.cloudfront.net/wp-content/u...", title: "Senior Software Engineer", bio: "Michael has been working as a back-end developer sin...", vote: 3, created_at: "2018-11-09 02:46:39", updated_at: "2018-11-09 02:46:39">
ruby-on-rails ruby
Have you tried<%= hidden_field_tag('roster[id]', roster.id) %>?
– jvillian
Nov 9 at 22:55
Yes, still getting same error.
– Sohel
Nov 10 at 19:12
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
Attempting to pass id's from my rails front end to back end. I have the following bit of code in my Rails controller:
def count_vote
roster_id = params[:roster_id]
roster = Roster.find(roster_id)
newvote = roster.vote + 1
if roster.update(vote: newvote)
redirect_to rosters_path
end
end
My rails view for roster has the following code:
<% @rosters.each do |roster| %>
<div class='each'>
<%= image_tag(roster['image_url'], class: 'image') %>
<%= hidden_field_tag(:roster_id, @roster.id) %>
<p class='name'> <%= roster['name'] %> </p>
<p class='title'> <%= roster['title'] %> </p>
<p> <%= roster['bio'] %> </p>
<p> <b> Want to work with <%= roster['name'] %>? </b> <%= link_to image_tag('thumbs-up.svg', class: 'thumbsup'), rosters_path, method: :patch %>
<br>
<%= roster['vote'] %> People have said Yes! </p>
<br>
</div>
<% end %>
I'm trying to pass the ID using the hidden field tag but I get the following error in my rails server logs:
Started PATCH "/rosters" for 127.0.0.1 at 2018-11-09 17:43:56 -0500
(0.2ms) SELECT "schema_migrations"."version" FROM "schema_migrations" ORDER BY "schema_migrations"."version" ASC
↳ /Users/sohel/.rbenv/versions/2.5.1/lib/ruby/gems/2.5.0/gems/activerecord-5.2.1/lib/active_record/log_subscriber.rb:98
Processing by RostersController#count_vote as HTML
Parameters: "authenticity_token"=>"DJAtI8yNTlP1kQK/g/6o9jbdqxWEBBgKinzLtf8v8WrDnYCmrH+HQI5wTEjJ0T6rkKbBz0KA/q2M0yirluozQg=="
Completed 404 Not Found in 14ms (ActiveRecord: 0.6ms)
ActiveRecord::RecordNotFound (Couldn't find Roster without an ID):
app/controllers/rosters_controller.rb:22:in `count_vote'
Searching Roster.all in my error console returns the following, for brevity sake I will return only the first two:
=> #<ActiveRecord::Relation [#<Roster id: 1, name: "John Smith", image_url: "https://d2eyrv63e6x6lp.cloudfront.net/wp-content/u...", title: "CTO / Co-founder", bio: "John has been programming since his dad bought him...", vote: 3, created_at: "2018-11-09 02:46:39", updated_at: "2018-11-09 02:46:39">, #<Roster id: 2, name: "Michael Thomas", image_url: "https://d2eyrv63e6x6lp.cloudfront.net/wp-content/u...", title: "Senior Software Engineer", bio: "Michael has been working as a back-end developer sin...", vote: 3, created_at: "2018-11-09 02:46:39", updated_at: "2018-11-09 02:46:39">
ruby-on-rails ruby
Attempting to pass id's from my rails front end to back end. I have the following bit of code in my Rails controller:
def count_vote
roster_id = params[:roster_id]
roster = Roster.find(roster_id)
newvote = roster.vote + 1
if roster.update(vote: newvote)
redirect_to rosters_path
end
end
My rails view for roster has the following code:
<% @rosters.each do |roster| %>
<div class='each'>
<%= image_tag(roster['image_url'], class: 'image') %>
<%= hidden_field_tag(:roster_id, @roster.id) %>
<p class='name'> <%= roster['name'] %> </p>
<p class='title'> <%= roster['title'] %> </p>
<p> <%= roster['bio'] %> </p>
<p> <b> Want to work with <%= roster['name'] %>? </b> <%= link_to image_tag('thumbs-up.svg', class: 'thumbsup'), rosters_path, method: :patch %>
<br>
<%= roster['vote'] %> People have said Yes! </p>
<br>
</div>
<% end %>
I'm trying to pass the ID using the hidden field tag but I get the following error in my rails server logs:
Started PATCH "/rosters" for 127.0.0.1 at 2018-11-09 17:43:56 -0500
(0.2ms) SELECT "schema_migrations"."version" FROM "schema_migrations" ORDER BY "schema_migrations"."version" ASC
↳ /Users/sohel/.rbenv/versions/2.5.1/lib/ruby/gems/2.5.0/gems/activerecord-5.2.1/lib/active_record/log_subscriber.rb:98
Processing by RostersController#count_vote as HTML
Parameters: "authenticity_token"=>"DJAtI8yNTlP1kQK/g/6o9jbdqxWEBBgKinzLtf8v8WrDnYCmrH+HQI5wTEjJ0T6rkKbBz0KA/q2M0yirluozQg=="
Completed 404 Not Found in 14ms (ActiveRecord: 0.6ms)
ActiveRecord::RecordNotFound (Couldn't find Roster without an ID):
app/controllers/rosters_controller.rb:22:in `count_vote'
Searching Roster.all in my error console returns the following, for brevity sake I will return only the first two:
=> #<ActiveRecord::Relation [#<Roster id: 1, name: "John Smith", image_url: "https://d2eyrv63e6x6lp.cloudfront.net/wp-content/u...", title: "CTO / Co-founder", bio: "John has been programming since his dad bought him...", vote: 3, created_at: "2018-11-09 02:46:39", updated_at: "2018-11-09 02:46:39">, #<Roster id: 2, name: "Michael Thomas", image_url: "https://d2eyrv63e6x6lp.cloudfront.net/wp-content/u...", title: "Senior Software Engineer", bio: "Michael has been working as a back-end developer sin...", vote: 3, created_at: "2018-11-09 02:46:39", updated_at: "2018-11-09 02:46:39">
ruby-on-rails ruby
ruby-on-rails ruby
asked Nov 9 at 22:50
Sohel
195
195
Have you tried<%= hidden_field_tag('roster[id]', roster.id) %>?
– jvillian
Nov 9 at 22:55
Yes, still getting same error.
– Sohel
Nov 10 at 19:12
add a comment |
Have you tried<%= hidden_field_tag('roster[id]', roster.id) %>?
– jvillian
Nov 9 at 22:55
Yes, still getting same error.
– Sohel
Nov 10 at 19:12
Have you tried
<%= hidden_field_tag('roster[id]', roster.id) %>?– jvillian
Nov 9 at 22:55
Have you tried
<%= hidden_field_tag('roster[id]', roster.id) %>?– jvillian
Nov 9 at 22:55
Yes, still getting same error.
– Sohel
Nov 10 at 19:12
Yes, still getting same error.
– Sohel
Nov 10 at 19:12
add a comment |
1 Answer
1
active
oldest
votes
up vote
4
down vote
hidden_field_tag(:roster_id, @roster.id)
should be
hidden_field_tag(:roster_id, roster.id)
Because there is no defined instance variable @roster
Also try to change this line to
<p> <b> Want to work with <%= roster['name'] %>? </b> <%= link_to image_tag('thumbs-up.svg', class: 'thumbsup'), rosters_path(roster), method: :patch %
I gave that a try, still get the same error?
– Sohel
Nov 10 at 19:11
The error I get with the hidden_field change is 'undefined method `id' for #<Hash:0x00007fc711750488>'
– Sohel
Nov 10 at 19:50
It won't read the params[:roster_id] in my controller, returns nil. Roster.find(1) however returns the different people by id number
– Sohel
Nov 10 at 20:36
check to make sureparams.permitallows the param you're passing in your controller
– lacostenycoder
Nov 10 at 21:20
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
4
down vote
hidden_field_tag(:roster_id, @roster.id)
should be
hidden_field_tag(:roster_id, roster.id)
Because there is no defined instance variable @roster
Also try to change this line to
<p> <b> Want to work with <%= roster['name'] %>? </b> <%= link_to image_tag('thumbs-up.svg', class: 'thumbsup'), rosters_path(roster), method: :patch %
I gave that a try, still get the same error?
– Sohel
Nov 10 at 19:11
The error I get with the hidden_field change is 'undefined method `id' for #<Hash:0x00007fc711750488>'
– Sohel
Nov 10 at 19:50
It won't read the params[:roster_id] in my controller, returns nil. Roster.find(1) however returns the different people by id number
– Sohel
Nov 10 at 20:36
check to make sureparams.permitallows the param you're passing in your controller
– lacostenycoder
Nov 10 at 21:20
add a comment |
up vote
4
down vote
hidden_field_tag(:roster_id, @roster.id)
should be
hidden_field_tag(:roster_id, roster.id)
Because there is no defined instance variable @roster
Also try to change this line to
<p> <b> Want to work with <%= roster['name'] %>? </b> <%= link_to image_tag('thumbs-up.svg', class: 'thumbsup'), rosters_path(roster), method: :patch %
I gave that a try, still get the same error?
– Sohel
Nov 10 at 19:11
The error I get with the hidden_field change is 'undefined method `id' for #<Hash:0x00007fc711750488>'
– Sohel
Nov 10 at 19:50
It won't read the params[:roster_id] in my controller, returns nil. Roster.find(1) however returns the different people by id number
– Sohel
Nov 10 at 20:36
check to make sureparams.permitallows the param you're passing in your controller
– lacostenycoder
Nov 10 at 21:20
add a comment |
up vote
4
down vote
up vote
4
down vote
hidden_field_tag(:roster_id, @roster.id)
should be
hidden_field_tag(:roster_id, roster.id)
Because there is no defined instance variable @roster
Also try to change this line to
<p> <b> Want to work with <%= roster['name'] %>? </b> <%= link_to image_tag('thumbs-up.svg', class: 'thumbsup'), rosters_path(roster), method: :patch %
hidden_field_tag(:roster_id, @roster.id)
should be
hidden_field_tag(:roster_id, roster.id)
Because there is no defined instance variable @roster
Also try to change this line to
<p> <b> Want to work with <%= roster['name'] %>? </b> <%= link_to image_tag('thumbs-up.svg', class: 'thumbsup'), rosters_path(roster), method: :patch %
edited Nov 10 at 19:30
answered Nov 9 at 22:55
lacostenycoder
3,56511226
3,56511226
I gave that a try, still get the same error?
– Sohel
Nov 10 at 19:11
The error I get with the hidden_field change is 'undefined method `id' for #<Hash:0x00007fc711750488>'
– Sohel
Nov 10 at 19:50
It won't read the params[:roster_id] in my controller, returns nil. Roster.find(1) however returns the different people by id number
– Sohel
Nov 10 at 20:36
check to make sureparams.permitallows the param you're passing in your controller
– lacostenycoder
Nov 10 at 21:20
add a comment |
I gave that a try, still get the same error?
– Sohel
Nov 10 at 19:11
The error I get with the hidden_field change is 'undefined method `id' for #<Hash:0x00007fc711750488>'
– Sohel
Nov 10 at 19:50
It won't read the params[:roster_id] in my controller, returns nil. Roster.find(1) however returns the different people by id number
– Sohel
Nov 10 at 20:36
check to make sureparams.permitallows the param you're passing in your controller
– lacostenycoder
Nov 10 at 21:20
I gave that a try, still get the same error?
– Sohel
Nov 10 at 19:11
I gave that a try, still get the same error?
– Sohel
Nov 10 at 19:11
The error I get with the hidden_field change is 'undefined method `id' for #<Hash:0x00007fc711750488>'
– Sohel
Nov 10 at 19:50
The error I get with the hidden_field change is 'undefined method `id' for #<Hash:0x00007fc711750488>'
– Sohel
Nov 10 at 19:50
It won't read the params[:roster_id] in my controller, returns nil. Roster.find(1) however returns the different people by id number
– Sohel
Nov 10 at 20:36
It won't read the params[:roster_id] in my controller, returns nil. Roster.find(1) however returns the different people by id number
– Sohel
Nov 10 at 20:36
check to make sure
params.permit allows the param you're passing in your controller– lacostenycoder
Nov 10 at 21:20
check to make sure
params.permit allows the param you're passing in your controller– lacostenycoder
Nov 10 at 21:20
add a comment |
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53234268%2frails-properly-pass-id-through-loop%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Have you tried
<%= hidden_field_tag('roster[id]', roster.id) %>?– jvillian
Nov 9 at 22:55
Yes, still getting same error.
– Sohel
Nov 10 at 19:12