Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
B
blog_devise
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
mis-21-2018-intersession
rails
blog_devise
Commits
0ac9c847
Commit
0ac9c847
authored
Jul 11, 2018
by
John Noel
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Connected User to Post
parent
f5ff1994
Changes
5
Show whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
25 additions
and
6 deletions
+25
-6
posts_controller.rb
app/controllers/posts_controller.rb
+4
-4
post.rb
app/models/post.rb
+2
-1
user.rb
app/models/user.rb
+10
-1
index.html.erb
app/views/posts/index.html.erb
+4
-0
20180711133212_add_users_to_posts.rb
db/migrate/20180711133212_add_users_to_posts.rb
+5
-0
No files found.
app/controllers/posts_controller.rb
View file @
0ac9c847
class
PostsController
<
ApplicationController
before_action
:authenticate_user!
def
index
@posts
=
Post
.
all
@posts
=
current_user
.
posts
end
def
show
...
...
@@ -9,11 +9,11 @@ class PostsController < ApplicationController
end
def
new
@post
=
Post
.
new
@post
=
current_user
.
posts
.
new
end
def
create
@post
=
Post
.
new
(
post_params
)
@post
=
current_user
.
posts
.
new
(
post_params
)
if
@post
.
save
redirect_to
posts_path
,
notice:
"You have successfully created a new post."
...
...
app/models/post.rb
View file @
0ac9c847
class
Post
<
ApplicationRecord
belongs_to
:user
end
\ No newline at end of file
app/models/user.rb
View file @
0ac9c847
...
...
@@ -3,4 +3,13 @@ class User < ApplicationRecord
# :confirmable, :lockable, :timeoutable and :omniauthable
devise
:database_authenticatable
,
:registerable
,
:recoverable
,
:rememberable
,
:trackable
,
:validatable
has_many
:posts
def
to_s
if
first_name
&&
last_name
"
#{
first_name
}
#{
last_name
}
"
else
email
end
end
end
\ No newline at end of file
app/views/posts/index.html.erb
View file @
0ac9c847
...
...
@@ -15,6 +15,7 @@
<thead>
<th>
Title
</th>
<th>
Content
</th>
<th>
Posted by
</th>
<th
colspan=
"3"
></th>
</thead>
<tbody>
...
...
@@ -26,6 +27,9 @@
<td>
<%=
post
.
content
%>
</td>
<td>
<%=
post
.
user
%>
</td>
<td>
<%=
link_to
"Show"
,
post_path
(
post
),
class:
"btn btn-primary"
%>
</td>
...
...
db/migrate/20180711133212_add_users_to_posts.rb
0 → 100644
View file @
0ac9c847
class
AddUsersToPosts
<
ActiveRecord
::
Migration
[
5.2
]
def
change
add_reference
:posts
,
:user
,
foreign_key:
true
end
end
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment