passing parameters through link_to method in ruby on rails 4 -
i have index view shows available classes want link "sign class" redirects me "sign up" form , fill other values , when sign created saves values filled plus foreign key of class passed via link method
classes index:
<h1>listof classes</h1> <table> <thead> <tr> <th>id</th> <th>name</th> <th>description</th> <th>actions</th> </tr> </thead> <tbody> <% @classes.each |class| %> <tr> <td><%= class.id %></td> <td><%= class.name %></td> <td><%= class.description %></td> <td><%= link_to 'sign up!', new_sign_up_path(:class_id => class.id), method: :post %></td> </tr> <% end %> </tbody> </table>
sign_up controller:
class signupscontroller < applicationcontroller before_action :set_sign_ups, only: [:show, :edit, :update, :destroy] # /sign_ups/new def new @sign_up = signup.new end def set_sign_ups @sign_up = signup.find(params[:id]) end def sign_up_params params.require(:sign_up).permit(:date, :status, :type, :class_id) end end
and sign ups form:
<%= form_for(@sign_up) |f| %> <div class="field"> <%= f.label :date %><br> <%= f.date_select :date %> </div> <div class="field"> <%= f.label :status %><br> <%= f.check_box :status %> </div> <div class="field"> <%= f.label :type %><br> <%= f.number_field :type %> </div> <div class="field"> <%= f.hidden_field :class_id %> </div> <div class="actions"> <%= f.submit %> </div> <% end %>
when save it doesn't save the class_id... i've tried parsing integer i've tried without hidden field allways passes blank en saving... ideas?
you passing in params[:class_id]
parameter on link_to
. form object class_id
attribute expecting @sign_up
object have class_id
attribute.
basically new
action you'll need set @sign_up
class_id
parameters:
def new @sign_up = signup.new @sign_up.class_id = params[:class_id] if params.has_key?(:class_id) end
this set class_id
parameter passed in link_to
url.
then form should work okay because f.hidden_field
@ current value of class_id
on @sign_up
, set value of input. doesn't matter haven't saved @ point - form_for
use unsaved state populate inputs.
Comments
Post a Comment