Make a note of the confusion while studying at TECH :: CAMP. I haven't read the official reference, so there may be some mistakes.
tl; d --You don't have to write anything in the index, show, new, edit actions. --form_with can be specified by model name and column name. --link_to can be passed as an instance with Prefix.
def index
end
def show
end
def new
end
def edit
end
Of the seven methods, I was surprised that these four would work if I wrote at least this much. In order to send data to the view side, I used the Active Record method to declare an instance variable ... and prepared it as I learned in the basic course. Certainly, it is almost certain that these actions will prepare instance variables of the corresponding model (in addition to id in the case of show and edit) when the action is specified by URL or Prefix. So even if you don't write it, it automatically prepares instance variables.
However, if you want to change the display order, load the related model with include to avoid the N + 1 problem, link with JavaScript, etc., you need to make a corresponding description.
html.erb
<%= form_with(model: @tweet, local: true) do |form| %>
<%= form.text_field :name, placeholder: "name" %>
<%= form.text_area :text, placeholder: "text" , rows: "10" %>
<%= form.submit "SEND" %>
<% end %>
It will automatically determine if the passed instance is in the record, so you don't have to write the path for which action to send the data. By doing this, the code of the form can be used as it is in the new view and the edit view. However, it seems that you need to new with the new action to tell form_with that this is an instance of new.
In the part that generates individual input form such as form.text_field: name
, if you specify the column name, it will automatically decide which column to save.
Moreover, when the symbol of this column name is converted to HTML, it is converted like ʻid = "name" , so even if you do not set the id for use in JavaScript, it is
document.getElementById (" Convenient to specify with name ")`.
html.erb
<% rooms.each do |room|%>
<%= link_to room.name, room_messages_path(room), method: :get%>
<% end %>
Since Prefix can specify an instance, there is no need to convert it to a numerical value or expand the expression like " rooms / destroy / # {room.id} "
.
Is it like a constant URI pattern? I was wondering, but who is the Prefix that can specify something like an argument? Method?
Recommended Posts