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
Hide 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
class
PostsController
<
ApplicationController
before_action
:authenticate_user!
before_action
:authenticate_user!
def
index
def
index
@posts
=
Post
.
all
@posts
=
current_user
.
posts
end
end
def
show
def
show
...
@@ -9,11 +9,11 @@ class PostsController < ApplicationController
...
@@ -9,11 +9,11 @@ class PostsController < ApplicationController
end
end
def
new
def
new
@post
=
Post
.
new
@post
=
current_user
.
posts
.
new
end
end
def
create
def
create
@post
=
Post
.
new
(
post_params
)
@post
=
current_user
.
posts
.
new
(
post_params
)
if
@post
.
save
if
@post
.
save
redirect_to
posts_path
,
notice:
"You have successfully created a new post."
redirect_to
posts_path
,
notice:
"You have successfully created a new post."
...
@@ -50,4 +50,4 @@ class PostsController < ApplicationController
...
@@ -50,4 +50,4 @@ class PostsController < ApplicationController
def
post_params
def
post_params
params
.
require
(
:post
).
permit!
params
.
require
(
:post
).
permit!
end
end
end
end
\ No newline at end of file
app/models/post.rb
View file @
0ac9c847
class
Post
<
ApplicationRecord
class
Post
<
ApplicationRecord
end
belongs_to
:user
end
\ No newline at end of file
app/models/user.rb
View file @
0ac9c847
...
@@ -3,4 +3,13 @@ class User < ApplicationRecord
...
@@ -3,4 +3,13 @@ class User < ApplicationRecord
# :confirmable, :lockable, :timeoutable and :omniauthable
# :confirmable, :lockable, :timeoutable and :omniauthable
devise
:database_authenticatable
,
:registerable
,
devise
:database_authenticatable
,
:registerable
,
:recoverable
,
:rememberable
,
:trackable
,
:validatable
:recoverable
,
:rememberable
,
:trackable
,
:validatable
end
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 @@
...
@@ -15,6 +15,7 @@
<thead>
<thead>
<th>
Title
</th>
<th>
Title
</th>
<th>
Content
</th>
<th>
Content
</th>
<th>
Posted by
</th>
<th
colspan=
"3"
></th>
<th
colspan=
"3"
></th>
</thead>
</thead>
<tbody>
<tbody>
...
@@ -26,6 +27,9 @@
...
@@ -26,6 +27,9 @@
<td>
<td>
<%=
post
.
content
%>
<%=
post
.
content
%>
</td>
</td>
<td>
<%=
post
.
user
%>
</td>
<td>
<td>
<%=
link_to
"Show"
,
post_path
(
post
),
class:
"btn btn-primary"
%>
<%=
link_to
"Show"
,
post_path
(
post
),
class:
"btn btn-primary"
%>
</td>
</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