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">









share|improve this question





















  • 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














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">









share|improve this question





















  • 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












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">









share|improve this question













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






share|improve this question













share|improve this question











share|improve this question




share|improve this question










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
















  • 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












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 %






share|improve this answer






















  • 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 sure params.permit allows the param you're passing in your controller
    – lacostenycoder
    Nov 10 at 21:20











Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













 

draft saved


draft discarded


















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

























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 %






share|improve this answer






















  • 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 sure params.permit allows the param you're passing in your controller
    – lacostenycoder
    Nov 10 at 21:20















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 %






share|improve this answer






















  • 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 sure params.permit allows the param you're passing in your controller
    – lacostenycoder
    Nov 10 at 21:20













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 %






share|improve this answer














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 %







share|improve this answer














share|improve this answer



share|improve this answer








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 sure params.permit allows 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










  • 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 sure params.permit allows 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


















 

draft saved


draft discarded















































 


draft saved


draft discarded














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





















































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







Popular posts from this blog

Ruanda

Makov (Slowakei)

Kleinkühnau