Rails: How to determine controller/action in view

前端 未结 5 1635
半阙折子戏
半阙折子戏 2021-01-31 17:10

This is my form partial:

<%= f.simple_fields_for :photo_attributes, :html => { :multipart => true } do |d| %>
    <%= d.label :image, :label =>         


        
5条回答
  •  我在风中等你
    2021-01-31 17:40

    You could write something like

    <%- form_url = @object.new_record? ? :photo_attributes : :photo %>
    <% f.simple_fields_for form_url, :html => { :multipart => true } do |d| %>
    

    That is, if you have an @object to check against. Otherwise you could use action_name (and even controller_name).

    So something like:

    <%- form_url = action_name == :edit ? :photo : :photo_attributes %>
    <% f.simple_fields_for form_url, :html => { :multipart => true } do |d| %>
    

    Hope this helps.

提交回复
热议问题