Если сделаете так с самого начала - все будет работать.
bundle install
rails g scaffold User name
user.rb
class User < ActiveRecord::Base
has_many :posts
has_many :comments
end
users_controller.rb
class UsersController < ApplicationController
before_action :set_user, only: [:show, :edit, :update, :destroy]
...
private
def set_user
@user = User.find(params[:id])
end
def user_params
params.require(:user).permit(:name)
end
end
rake db:migrate
----
rails g scaffold Post title body:text user:references
rake db:migrate
post.rb
class Post < ActiveRecord::Base
belongs_to :user
has_many :comments
end
posts_controller.rb
class PostsController < ApplicationController
...
def post_params
params.require(:post).permit(:title, :body, :user_id)
end
end
posts/_form.html.erb
<%= f.hidden_field :user_id, :value => current_user.id %>
<%= post.user.name %> ваш спаситель
-----
rails g scaffold Comment body:text user:references post:references
rake db:migrate
comment.rb
class Comment < ActiveRecord::Base
belongs_to :user
belongs_to :post
end
comments_controller.rb
class PostsController < ApplicationController
...
def post_params
params.require(:post).permit(:body, :user_id, :post_id)
end
end
------