开发者

Rails3 - How to make a submit button for a nested resource

开发者 https://www.devze.com 2023-01-30 09:20 出处:网络
I\'m using SimpleForm 开发者_StackOverflow中文版in Rails 3. How do I make the submit button for this nested resource?

I'm using SimpleForm 开发者_StackOverflow中文版in Rails 3. How do I make the submit button for this nested resource?

resources :schools do   
  resources :students
end

<%= simple_form_for @student do |f| %>
  <%= f.input :first_name %>
  <%= f.input :last_name %>
  <%= f.button :submit %>
<% end %>    

If I use f.submit:

ActionView::Template::Error (undefined method `students_path' for #<#<Class:0x000001040ddfb8>:0x000001040d2578>):
1: <%= simple_form_for @student do |f| %>
2:     <%= f.input :first_name %>
3:     <%= f.input :last_name %>
4:     <%= f.submit %>


The right code for the view is:

<%= simple_form_for [@school, @student] do |f| %>
     <%= f.input :first_name %>
     <%= f.input :last_name %>
     <%= f.button :submit  %>
<% end %>


Just use <%= f.submit %> instead of <%= f.button :submit %>


Not sure how or where, you are setting the @school. if @school is nil the above answer may not work.

However you can also use

<%= simple_form_for [:school, @student] do |f| %>
 <%= f.input :first_name %>
 <%= f.input :last_name %>
 <%= f.submit  %>
<% end %>
0

精彩评论

暂无评论...
验证码 换一张
取 消