[RUBY] Rails does it automatically and it helps, but I was surprised

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.

Rails will do it for you

You don't have to write anything in the index, show, new, edit actions

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.

form_with can be specified by model name and column name.

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 ")`.

For link_to, pass the instance with Prefix.

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

Rails does it automatically and it helps, but I was surprised
I entered from Rails and didn't know what [attr_accessor] was
I built a rails environment with docker and mysql, but I got stuck
Rails Tutorial Chapter 14 was completed, and it took a total of 155.5 hours.
I was charged a lot by AWS during development on rails, but ...
I was a little addicted to running old Ruby environment and old Rails
I can deploy Heroku, but I get an error and can't open it
I put composer, but it doesn't respond.
I created a Rails post form, but I can't post it (form tag) / No error occurs