codeinaire
codeinaire

Reputation: 1864

Relationship of request to routes and actions

I'm learning Ruby on Rails. I decided to conceptually and concretely follow the transfer of information from the client to the server through the Rails application and back to the client as a response.

In the flow of info I'm following I click on the "New Post" link which routes through the posts controller and the new action. This is what's in the new action: @post = current_user.posts.build. My thinking is that this creates an object in memory (.build) with the posts table connected to the current_user (from Devise). The current user is data comes from the user table which comes through the models. This object is assigned to the @post variable.

I'm not 100% clear of the relationship between current_user and posts, but it seems to me that posts is included in current_user. Is there a better way to conceptually see this??

This @post object is sent, along with the new view, to the client. In the new view is this:

<%= simple_form_for @post, html: { class: 'form-horizontal', multipart: true } do |f| %>
<div class="form-group">
  <%= f.input :image %>
</div>
<div class="form-group text-center">
  <%= f.input :caption %>
</div>
<div class="form-group text-center">
  <%= f.button :submit, class: 'btn-success' %>
</div>
<% end %>

I don't entirely understand what is going on here. I think the @post variable is plugged into this do loop and each of these inputs are put into the image and caption columns, plus info is put into the other columns. This is possible because the @post object contains the columns from the posts table. Am I correct in thinking what happens here??

Now, with the image and caption in this object the client presses submit and starts another request. This request uses the POST method and so it is routed to the posts controller and to the create action. This is the first line in the create action: @post = current_user.posts.build(post_params).

This is where my understanding starts to fall apart. I don't understand how the @post object with the image and caption "inside" of it enters the create action. Does the route send it directly into the create action? Is this why the create action doesn't have an argument? At least mine doesn't and I don't know if actions can take arguments. Can they??

Once inside the create action what is happening with the @post object inside of which is the image, caption, and other data? Is it being replaced by whatever is inside current_user.posts.build(post_params)? And what is actually inside of current_user.posts.build(post_params)? Does the @post object - which is this current_user.posts.build - take the place of current_user.posts.build(post_params)?

The post_params argument sent to build is this method:

def post_params
   params.require(:post).permit(:image, :caption)
end

I think this method is making current_user.posts.build is white-listing the image and caption columns for mass migration in the post table.

Thanks in advance to anyone that can help me understand this more clearly!

Upvotes: 1

Views: 28

Answers (1)

SoWhat
SoWhat

Reputation: 5622

relationship between current_user and posts, If you open the user.rb model you will see a has_many association

has_many :posts

A user has many posts and that's why you can do user.posts.build

<%= simple_form_for @post, html: { class: 'form-horizontal', multipart: true } do |f| %>

This is not a loop. Its a block. A block is similar to a callback. f is the form builder object on @post f.input (specifies input tag of correct type) :caption is the field on @post that you want to build a form around

POST method has nothing to do with your model post FYI. its an HTTP method and would remain the same for any other model

@post = current_user.posts.build(post_params).

post_params filters the params (form tdata) that is passed and allows only specific fields. When you submit the form, the data is made available in params in the post key params[:post][:caption] ...

.build is passed the parameters which creates a new instance of model with the entered data when you use .save it saves this data to the db

Upvotes: 1

Related Questions