[Ruby On Rails] In the nested state, the description in parentheses written after Prefix in the link_to method

I'm sorry. It is a complete memorandum. .. Lol

Premise

rubyonrails:show.html.erb


  <% @fuga_events.each do |event| %>
  ----------------------------------------
    <ul>
      <div>
        <%= "name:#{event.hoge.name}" %>
      </div>
      <div>
        <%= "App:#{event.hoge.app_name}" %>
      </div>
      <div>
        <%= "start date:#{event.started_at}"%>
      </div>
      <div>
        <%= "End date:#{event.finished_at}" %>
      </div>
      <div>
        <%= "todo:#{event.todo}" %>
      </div>
      <div>
        <%= "place:#{event.place}" %>
      </div>
      <div>
        <%= "Expected:#{event.expected_reward}" %>
      </div>
      <div>
        <%= "Reward:#{event.reward}" %>
      </div>
    </ul>
    <div>
      <%= link_to "To edit", edit_hoge_fuga_event_path(), method: :get %>
      <%= link_to "delete", hoge_fuga_event_path(), method: :delete %>
    </div>
  <% end %>

This time, I want to describe the id (key) of the parameter I want to pass in (). In the above, hoge is the parent and fuga_events is the child nested state as follows.

routes.rb


(Omitted)

    resources :hoges, except: [:index] do
      resources :fuga_events, except: [:index]
    end

(Omitted)

Actually, what kind of description is required in the Prefix (path) described in the link_to method, in this case in the parentheses after edit_hoge_fuga_event_path and hoge_fuga_event_path?

How to write

rubyonrails:show.html.erb


<%= link_to "To edit", edit_hoge_fuga_event_path(event.papa_id, event.id), method: :get %>
<%= link_to "delete", hoge_fuga_event_path(event.papa_id, event.id), method: :delete %>

(Event.papa_id, event.id) are described in the edit (edit action) and delete (destroy action) functions, respectively.

Way of thinking

I have red </ font> as the nesting parent and blue </ font> as the nesting child, and describe the corresponding key for each. I thought.

<% = link_to "edit", edit_ hoge </ font> _ fuga_event </ font> _path ( event.papa_id </ font>, event.id </ font>), method :: get%>

<% = link_to "Delete", hoge </ font> _ fuga_event </ font> _path ( event.papa_id < / font>, event.id </ font>), method :: delete%>

Notice

This way of writing is not always the case. After all, as a way of thinking, I realized that it is important to specify what kind of id is passed in the parentheses written after the path.

Recommended Posts