About linking user information to posts in Ruby on Rails

Asked 2 years ago, Updated 2 years ago, 112 views

■Prerequisites/What you want to realize


in Ruby on Rails We create a simple blog that allows multiple users to post articles.
Anyone can log in using email and password and
I'm making it possible to post articles.

And what I want to implement now is
"When you go to the article page, you can see who posted the article by email address."
That's right.

For example,

The article that A logged in and posted on [email protected] is
"Writer: [email protected]"
appears and

The article that Mr. B logged in and posted on [email protected] is
"Writer: [email protected]"
I'd like to see

As for the user, I am using device.

For example in the image,
If you log in and post an article as shown below,

Enter a description of the image here

I would like to see the person who wrote it like this on the article page.

Enter a description of the image here

Also, when I edited it, I didn't change the email display.
I want to do whatever I want.

In other words,
The article that A logged in and posted on [email protected] is
"Writer: [email protected]"
It says
but even if Mr. B edits this article,

Stay
Writer: [email protected] That's what it looks like.

=================================

To implement this
First add has_many:users to the post and associate it.


in the article data (post) Add the column user_id and

The id of the person who logs in and posts the article is

in the html.erb section of the view after substituting and saving.
Posters: <%@post.user.email%>
I tried, but it didn't work.


when you log in and actually try to post an article The following error occurred:

■Problems/Error Messages Occurred

Enter a description of the image here

■ Affected Source Code

The controller should look like this:


in application_controller.rb Add the code as follows

before_action:set_current_user, only:[:create]

def set_current_user
@current_user=User.find_by (id:session[:user.id])
end

● This is what posts_controller.rb says.

class PostsController<ApplicationController
def index
@post=Post.all.order(created_at:'desc')
end

def show
@post=Post.find_by (params[:id])
@user=User.find_by (id:@post.user_id)
end

def new
@post=Post.new
end

def create
@post=Post.new(
content:params[:content],
user_id:@current_user.id

[email protected]
redirect_to root_path
else
render 'new'
end
end

default
@post=Post.find (params[:id])
end

default update
@post=Post.find (params[:id])
[email protected](post_params)
redirect_to post_path
else
render 'edit'
end
end

def post_params
param.require(:post).permit(:title,:body)
end
end

●The parts written in show.html.erb are as follows:

<p>Writer:<%[email protected]%>/p>

I will also list the post.rb of the ● model.

class Post <ApplicationRecord
values:title, presence:true
values:body, presence:true
has_many —users
end

●db looks like this

ActiveRecord::Schema.define (version: 20171209035931) do

create_table "posts", force: :cascade do | t |
t.string "title"
t.text "body"
t.datetime "created_at", null:false
t.datetime "updated_at", null:false
t. integer "user_id"
end

create_table "users", force: :cascade do | t |
t.string "email", default:", null:false
t.string "encrypted_password", default:", null:false
t.string "reset_password_token"
t.datetime "reset_password_sent_at"
t.datetime "remember_created_at"
t. integer "sign_in_count", default:0, null:false
t.datetime "current_sign_in_at"
t.datetime "last_sign_in_at"
t.string "current_sign_in_ip"
t.string "last_sign_in_ip"
t.datetime "created_at", null:false
t.datetime "updated_at", null:false
t.index["email", name: "index_users_on_email", unique: true
t.index["reset_password_token", name: "index_users_on_reset_password_token", unique: true
end

end

■Tried

I tried as written on ↓'s website.
https://qiita.com/you88/items/63ad0b9c07da4323fe26

However, it didn't work, so I looked into various things
I tried to fiddle with variables, but it didn't work.

■ Supplementary information (language/FW/tool version, etc.)

I thought it would be a supplement, but
The error id is nil Class, but
Logged in to @current_user in the first place
User value may not be included
I am thinking that

I'm in trouble.
Thank you for your cooperation.

==============================

add

Thank you for your reply.

As you pointed out, has_many was wrong.
Thank you.
However, it is clogged with other parts, so
I would appreciate it if you could help me a little more

This is how I'm going to write an
When I try to post
This error occurs.

Enter a description of the image here

As I wrote above,
The controller is

in application_controller.rb

before_action:set_current_user, only:[:create]

default_current_user
@current_user=User.find_by (id:session[:user_id])
end

in post_controller.rb

def create
@post=Post.new(
post_params,
user_id:current_user.id

[email protected]
redirect_to root_path
else
render 'new'
end
end

says .


if there is a solution Please let me know.
Thank you for your cooperation.

==================================================

Additional 2

Thank you.
I decided to try the former one.

However, log in and
I was about to post an article
If current_user.id is nil as follows:
I was told.

Enter a description of the image here

Only while logged in

as I have made it impossible to post. I'm sure you're logged in, but
Why does this happen?

It's a selfish imagination, but
If you are using gem device,

in application_controller.rb

before_action:set_current_user, only:[:create]

default_current_user
@current_user=User.find_by (id:session[:user_id])
end

You can't use the part
Could it happen?

Sorry to keep bothering you, but
I look forward to your kind cooperation.

====================================

Additional 3

In application_controller.rb

before_action:set_current_user, only:[:create]

default_current_user
@current_user=current_user
end
However, if there is an error in posting an article,
It's gone!

However, if you go to the pages of each article,

Enter a description of the image here

It will be

This means that the post has user information
Does that mean it's not stuck together?
What should I do...
I look forward to your kind cooperation.

ruby-on-rails ruby devise

2022-09-30 20:12

1 Answers

On the Post model,

 has_many:users

not

belongs_to:user

as

belongs_to when referring to other tables in columns that record IDs in other tables, or has_many or has_one (1-to-many or 1:1) when referring to other tables in columns that record IDs in other tables.

The reason for the error is that the argument Post.new is incorrect (cannot pass arguments like code).

Simply

@post=Post.new(post_params)
@post.user_id=@current_user.id

works with , but if you want to do something like a question,

Add a Post Relationship to the User Model and

class User<...
  has_many —posts
end

I think you should use that relationship to create a Post

@current_user.posts.build(post_params)


2022-09-30 20:12

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.