mnort9
mnort9

Reputation: 1820

Passing model id through rails route in form

I'm calling a custom action with simple_form. I'm having trouble passing the :id parameter to the action.

routes

post '/posts/:id/admin_vote' => 'posts#admin_vote', as: 'admin_vote'

form

<%= simple_form_for :post, url: admin_vote_path(:post_id), :html => {:class => 'form-inline admin-vote-form'} do |f| %>
  <%= f.select :vote, 1..20 %>
  <%= f.submit 'Vote', :class => 'btn btn-primary btn-xs' %>
<% end %>

partial render

<%= render 'layouts/admin_vote', :locals => { :post => post, :post_id => post.id } %>

For some reason the action receives params[:id] = 'post_id' instead of the actual id.

Upvotes: 1

Views: 143

Answers (2)

gotva
gotva

Reputation: 5998

it seems to me your form should be

<%= simple_form_for post, url: admin_vote_path, :html => {:class => 'form-inline admin-vote-form'} do |f| %>
  <%= f.select :vote, 1..20 %>
  <%= f.submit 'Vote', :class => 'btn btn-primary btn-xs' %>
<% end %>

you should build form based on variable post

Upvotes: 0

mechanicalfish
mechanicalfish

Reputation: 12826

You're providing :post_id symbol to the admin_vote_path, so it uses that. Change it to:

admin_vote_path(params[:post_id])

or a different parameter depending on the context of your form.

Upvotes: 1

Related Questions