简体   繁体   中英

DRY Rails Forms - Form Partials

I'm having problems with repetetive Rails form codes. When I put them in a partial they are throwing NoMethodError and I couldn't figure it out how to make them DRYer.

admin/exam_centers/edit.html.erb =>

<%= simple_form_for(@exam_center, url: admin_exam_center_path(@exam_center)) do |f| %>
  <%= f.input :city_id, collection:City.all, label: 'Şehir Seçiniz', label_method: :name %>
  <%= f.input :address, label: 'Adres' %>
  <%= f.input :building, label: 'Bina' %>
  <%= f.button :submit, "Kaydet", :class => "btn btn-info" %>
<% end %>

Notice: admin_exam_center_path(@exam_center)

admin/exam_centers/new.html.erb =>

<%= simple_form_for(@exam_center, url: admin_exam_centers_path(@exam_center)) do |f| %>
  <%= f.input :city_id, collection:City.all, label: 'Şehir Seçiniz', label_method: :name %>
  <%= f.input :address, label: 'Adres' %>
  <%= f.input :building, label: 'Bina' %>
  <%= f.button :submit, "Kaydet", :class => "btn btn-info" %>
<% end %>

Notice: admin_exam_centers_path(@exam_center)

They are working perfect like this. But when I put them in a partial as you can see below, the new.html.erb page is throwing "undefined method exam_centers_path" error and the edit.html.erb page is throwing "undefined method exam_center_path" error. How can I make these form DRYer?

_form.html.erb =>

<%= simple_form_for(@exam_center) do |f| %>
  <%= f.input :city_id, collection:City.all, label: 'Şehir Seçiniz', label_method: :name %>
  <%= f.input :address, label: 'Adres' %>
  <%= f.input :building, label: 'Bina' %>
  <%= f.button :submit, "Kaydet", :class => "btn btn-info" %>
<% end %>

edit.html.erb =>

<%= render 'admin/exam_centers/form' %>

new.html.erb =>

<%= render 'admin/exam_centers/form' %>

By the way, the routes file =>

  namespace :admin do
   resources :exam_centers, except: :show
   resources :exam_languages, except: :show
  end

It looks like you just need to use a polymorphic url builder to specify the :admin piece of your route. Try specifying the form helper like this:

<%= simple_form_for([:admin, @exam_center]) do |f| %>

This will allow Rails to tack on the admin_ piece of the named route before generating the rest.

You only put common part in partials so you can do something like this:

new.html.erb

<%= simple_form_for(@exam_center, url: admin_exam_centers_path(@exam_center)) do |f| %>
  <%= render partial: "form", locals: {:f => f} %>
<% end %>

edit.html.erb

<%= simple_form_for(@exam_center, url: admin_exam_center_path(@exam_center)) do |f| %>
  <%= render partial: "form", locals: {f: "f"} %>
<% end %>

_form.html.erb

<%= f.input :city_id, collection:City.all, label: 'Şehir Seçiniz', label_method: :name %>
<%= f.input :address, label: 'Adres' %>
<%= f.input :building, label: 'Bina' %>
<%= f.button :submit, "Kaydet", :class => "btn btn-info" %>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM