Rails : undefined method `user_id=' for nil:NilClass - ruby-on-rails

I am running into a problem with the Comments controller in my blog. Every time I tried to create a new comment I have an undefined method "user_id=" for nil:NilClass, and it doesn't create the comment. It happens the same when I try to destroy one, I have undefined method "comments" for #<User:0x007fe2c872f968>.
When I restart the server, the comment I created shows up, but it doesn't happen the same with the destroy method.
I am not sure what is happening here, I have to be doing something wrong with the user but I cannot find where is the problem.
Posts Controller
class PostsController < ApplicationController
before_action :authenticate_user!, except: [:index, :show]
def index
#posts = Post.all.order('created_at DESC')
#posts = Post.paginate(:page => params[:page])
end
def new
#post = Post.new
end
def create
#post = Post.new(post_params)
if #post.save
redirect_to #post
else
render "new"
end
end
def show
#post = Post.find(params[:id])
end
def edit
#post = Post.find(params[:id])
end
def update
#post = Post.find(params[:id])
if #post.update(params[:post].permit(:title, :body, :image))
redirect_to #post
else
render 'edit'
end
end
def destroy
#post = Post.find(params[:id])
#post.destroy()
redirect_to posts_path
end
private
def post_params
params.require(:post).permit(:title, :body, :image)
end
end
Comments Controller
class CommentsController < ApplicationController
before_action :correct_user, only: :destroy
def create
#post = Post.find(params[:post_id])
#comments = #post.comments.create(params[:comment].permit(:name, :body))
#comment.user_id = current_user.id
comment.save
redirect_to post_path(#post)
end
def show
#post = Post.find(params[:post_id])
#comments = #post.comments.create(params[:comment])
end
def destroy
#post = Post.find(params[:post_id])
#comments = #post.comments.find(params[:id])
#comments.destroy
redirect_to post_path(#post)
end
private
def comment_params
params.require(:commet).permit(:user_id, :name, :body)
end
def correct_user
#comment = current_user.comments.find_by(id: params[:id])
if #comment.nil?
flash[:alert] = "Not your comment!"
redirect_to :back
end
end
end
Schema
ActiveRecord::Schema.define(version: 20160515190759) do
create_table "average_caches", force: :cascade do |t|
t.integer "rater_id"
t.integer "rateable_id"
t.string "rateable_type"
t.float "avg", null: false
t.datetime "created_at"
t.datetime "updated_at"
end
create_table "comments", force: :cascade do |t|
t.string "name"
t.text "body"
t.integer "post_id"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.integer "user_id"
end
add_index "comments", ["post_id"], name: "index_comments_on_post_id"
create_table "overall_averages", force: :cascade do |t|
t.integer "rateable_id"
t.string "rateable_type"
t.float "overall_avg", null: false
t.datetime "created_at"
t.datetime "updated_at"
end
create_table "posts", force: :cascade do |t|
t.string "title"
t.text "body"
t.string "image_url"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.string "image_file_name"
t.string "image_content_type"
t.integer "image_file_size"
t.datetime "image_updated_at"
end
create_table "rates", force: :cascade do |t|
t.integer "rater_id"
t.integer "rateable_id"
t.string "rateable_type"
t.float "stars", null: false
t.string "dimension"
t.datetime "created_at"
t.datetime "updated_at"
end
add_index "rates", ["rateable_id", "rateable_type"], name: "index_rates_on_rateable_id_and_rateable_type"
add_index "rates", ["rater_id"], name: "index_rates_on_rater_id"
create_table "rating_caches", force: :cascade do |t|
t.integer "cacheable_id"
t.string "cacheable_type"
t.float "avg", null: false
t.integer "qty", null: false
t.string "dimension"
t.datetime "created_at"
t.datetime "updated_at"
end
add_index "rating_caches", ["cacheable_id", "cacheable_type"], name: "index_rating_caches_on_cacheable_id_and_cacheable_type"
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
end
add_index "users", ["email"], name: "index_users_on_email", unique: true
add_index "users", ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true
end

All simple, you have two different instance objects. Also, You have already comment_params method, just use that one for secure it.
def create
#post = Post.find(params[:post_id])
#comment = #post.comments.create(comment_params.merge(user_id: current_user.id))
redirect_to post_path(#post)
end

try:
#comments = #post.comments.create(params[:comment].permit(:name, :body))
just replace #comments with #comment

Regarding your create method. You have two problems:
You don't have #comment instance variable defined (you have #comments).
You don't have comment local variable or method.
comments_controller.rb
class CommentsController < ApplicationController
def create
#post = Post.find(params[:post_id])
# |-- HERE
#comment = #post.comments.create(params[:comment].permit(:name, :body))
#comment.user_id = current_user.id
#comment.save # <== HERE ==
redirect_to post_path(#post)
end
end
As for destroy, you might have forgot to add has_many :comments associated to your User model.
class User < ActiveRecord::Base
has_many :comments
end
class Comment < ActiveRecord::Base
belongs_to :user
end

Related

Why I can't create article with any category?

I'm new in RoR, and I'm working on Blog app, and implementing categories for articles. But I have trouble - when I create any article with some categories ('sport' or 'movie' or any other) I receive validation errors
- Category must exist
- Category can't be blank
But I have working dropdown list or categories (this helper):
def categories
category =
["Sport",
"Movie",
"Art",
"Nature",
"Exotic"]
category.each do |categ|
my_category = "#{categ}"
end
return category
end
And here is a piece of code of my article.new.html.erb file:
<p>
<%= f.label :category %><br>
<%= f.select :category, categories,
prompt: "Choose your category" %>
</p>
Also here is my db schema where categories fields are present:
create_table "articles", force: :cascade do |t|
t.string "title"
t.text "text"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.string "pic"
t.string "photo_file_name"
t.string "photo_content_type"
t.bigint "photo_file_size"
t.datetime "photo_updated_at"
t.string "music_file_name"
t.string "music_content_type"
t.bigint "music_file_size"
t.datetime "music_updated_at"
t.string "movie_file_name"
t.string "movie_content_type"
t.bigint "movie_file_size"
t.datetime "movie_updated_at"
t.string "category_id"
end
create_table "categories", force: :cascade do |t|
t.string "name"
t.text "desc"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
create_table "comments", force: :cascade do |t|
t.string "commenter"
t.text "body"
t.bigint "article_id"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.index ["article_id"], name: "index_comments_on_article_id"
end
create_table "subscribers", force: :cascade do |t|
t.string "f_name"
t.string "l_name"
t.string "email"
t.string "country"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
create_table "users", force: :cascade do |t|
t.string "userid"
t.string "email"
t.string "password_digest"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.boolean "admin", default: false
end
add_foreign_key "comments", "articles"
end
And here is my models:
class Article < ApplicationRecord
belongs_to :category
has_many :comments, dependent: :destroy
validates :title, presence: true, length: {minimum: 3}
validates :text, presence: true, length: {minimum: 3}
validates :category_id, presence: true
end
class Category < ApplicationRecord
has_many :articles
end
Also this is my Article controller:
class ArticlesController < ApplicationController
before_action :admin_authorize, :except => [:index, :show, :search]
def index
#articles = Article.includes(:category).order("created_at DESC")
if params[:category].blank?
#articles = Article.all.order("created_at DESC")
else
#category_id = Category.find_by(name: params[:category]).id
#articles = Article.where(category_id: #category_id).order("created_at DESC")
end
end
def new
#article = Article.new
#categories = Category.all.map{|c| [c.name, c.id]}
end
def show
#article = Article.find(params[:id])
end
def create
#article = Article.new(article_params)
#article.category_id = params[:category_id]
respond_to do |format|
if #article.save
format.html { redirect_to #article, notice: "Article was successfully created!" }
format.json { render :show, status: :created, location: #article }
else
format.html { render :new}
format.json {render json: #article.errors, status: :unprocessable_entity}
end
end
end
def edit
#article = Article.find(params[:id])
#categories = Category.all.map{|c| [ c.name, c.id ] }
end
def search
if params[:search].blank?
#articles = Article.all
else
#articles = Article.search(params)
end
end
def update
#article = Article.find(params[:id])
#article.category_id = params[:category_id]
if #article.update(article_params)
redirect_to #article
else
render 'edit'
end
end
def destroy
#article = Article.find(params[:id])
#article.destroy
redirect_to articles_path
end
private
def article_params
params.require(:article).permit(:title, :text, :search, :music, :movie, :photo)
end
def find_article
#article = Article.find(params[:id])
end
end
Thanks in advance!!
You forgot the category_id in your permitted params:
def article_params
params.require(:article).permit(:title, :text, :search, :music, :movie, :photo, :category_id)
end
You also need to change your select helper to send category_id and not category in the POST request.
Now, with your categories helper, you don't send any category id in your select dropdown, just some "category" names which are not bound to any Category instances.
You can fix the select like this and remove your helper:
<p>
<%= f.label :category %><br>
<%= f.select :category_id, Category.all.collect{|c| [c.name, c.id]},
prompt: "Choose your category" %>
</p>

Can't access another model through Active Record - nil class?

I'm very new to ruby and to rails.
I've been stuck with this for days. I think I should be able to access a variable in a controller of one model, that is stored in another model easily?
I have four models: User - (1-1) - Profile - (1-M) - Appointment - (1-1) - Option.
Options contains pricePerPerson and discount for an appointment. Appointments contains, among others, numPeople.
I want to access the figure in the Options model for the pricePerPerson (and discount, if appropriate) and use it in the view show for Appointments.
(# <%= appointment.price = Option.pricePerPerson * numPeople %>)
So far I haven't been able to do this and have had to resort to hard-coding a price per person. I would rather not hard-code that, if possible.
So far, the system is saying that there is nothing in the database(nil class for price. I don't know why as I have entered a figure in this. It is skipping to the "else" section of the method. (Basically, it keeps returning 6, no matter what)
Appointments.rb Model:
def calculatePrice
if option.present?
return option.pricePerPerson*numpeople
else
return 6
end
end
Appointments controller:
def show
#appointment = Appointment.find(params[:id])
end
def new
#appointment = Appointment.new
#appointment.price
end
def create
#appointment = Appointment.new(appointment_params)
respond_to do |format|
if #appointment.save
format.html { redirect_to #appointment, notice: 'Appointment was
successfully created.' }
format.json { render :show, status: :created, location: #appointment }
else
format.html { render :new }
format.json { render json: #appointment.errors, status:
:unprocessable_entity }
end
end
end
Options controller:
def new
#option = Option.new
end
def create
#option = Option.new(option_params)
respond_to do |format|
if #option.save
format.html { redirect_to #option, notice: 'Option was successfully
created.' }
format.json { render :show, status: :created, location: #option }
else
format.html { render :new }
format.json { render json: #option.errors, status: :unprocessable_entity
}
end
end
end
options.rb model:
class Option < ActiveRecord::Base
belongs_to :appointment
end
Database schema:
ActiveRecord::Schema.define(version: 20160703171518) do
create_table "appointments", force: :cascade do |t|
t.string "name"
t.string "phone"
t.string "email"
t.integer "numpeople"
t.date "date"
t.string "timeslot"
t.integer "user_id"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
add_index "appointments", ["user_id"], name: "index_appointments_on_user_id"
create_table "options", force: :cascade do |t|
t.decimal "pricePerPerson"
t.integer "discount"
t.integer "appointment_id"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
add_index "options", ["appointment_id"], name
"index_options_on_appointment_id"
create_table "profiles", force: :cascade do |t|
t.string "firstname"
t.string "lastname"
t.text "address"
t.string "email"
t.string "phone"
t.integer "user_id"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
add_index "profiles", ["user_id"], name: "index_profiles_on_user_id"
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
end
add_index "users", ["email"], name: "index_users_on_email", unique: true
add_index "users", ["reset_password_token"], name:
"index_users_on_reset_password_token", unique: true
end
I agree with the comments. It's likely that you can simplify your database schema.
However, should you want to stick to your schema and should you want to be able to debug your code by yourself (in simple cases), I suggest you to install the following gems in you Gemfile. When there's something wrong in your code, it will help you understand where and will display a console allowing you to test some new code.
group :development, :test do
gem 'binding_of_caller'
gem 'better_errors'
gem 'pry-byebug'
end

Heroku Error Deleting a Post in Production: Rails

I am trying to delete a post in my app. It's working fine in localhost but when i pushed to heroku it's not working. I get an error saying "Something went wrong , Please check the logs". Here is my code:
posts_controller.rb
class PostsController < ApplicationController
before_action :set_post, only: [:show, :edit, :update, :destroy]
before_action :authenticate_user! , except: [:index,:show,:search]
before_filter :check_user, only: [:edit,:update,:destroy]
# GET /posts
# GET /posts.json
def search
if params[:search].present?
#posts = Post.search(params[:search])
else
#posts = Post.all
end
end
def index
if params[:tag]
#posts = Post.tagged_with(params[:tag])
else
#posts = Post.all
end
end
# GET /posts/1
# GET /posts/1.json
def show
#reviews = Review.where(post_id: #post.id)
end
# GET /posts/new
def new
#post = Post.new
end
# GET /posts/1/edit
def edit
#post = Post.find(params[:id])
end
# POST /posts
# POST /posts.json
def create
#post = Post.new(post_params)
#post.user_id = current_user.id
respond_to do |format|
if #post.save
format.html { redirect_to #post, notice: 'Post was successfully created.' }
format.json { render :show, status: :created, location: #post }
else
format.html { render :new }
format.json { render json: #post.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /posts/1
# PATCH/PUT /posts/1.json
def update
respond_to do |format|
if #post.update(post_params)
format.html { redirect_to root_url, notice: 'Post was successfully updated.' }
format.json { render :show, status: :ok, location: #post }
else
format.html { render :edit }
format.json { render json: #post.errors, status: :unprocessable_entity }
end
end
end
# DELETE /posts/1
# DELETE /posts/1.json
def destroy
#post.destroy
respond_to do |format|
format.html { redirect_to root_path, notice: 'Post was successfully destroyed.' }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_post
#post = Post.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def post_params
params.require(:post).permit(:title, :description,:image,:all_tags)
end
def check_user
if current_user.id != #post.user_id
redirect_to root_path , alert: "Sorry this Post belongs to someone else"
end
end
end
The Logs
view/posts/index.html.erb
<h3>Posts</h3>
<table class="table">
<thead>
<tr>
<th colspan="3"></th>
</tr>
</thead>
<tbody>
<% #posts.each do |post| %>
<tr>
<td><h4><%=link_to post.title , post%></h4></td>
<td><%=raw tag_links(post.all_tags)%></td>
<td><%= link_to 'Edit', edit_post_path(post) %></td>
<td><%= link_to 'Destroy', post, method: :delete, data: { confirm: 'Are you sure?' } %></td>
</tr>
<%end%>
</tbody>
</table>
models/post.rb
class Post < ActiveRecord::Base
searchkick
has_many :reviews , dependent: :destroy
has_many :taggings, dependent: :destroy
has_many :tags, through: :taggings
#Paperclip Installation
has_attached_file :image, styles: { medium: "300x300>", thumb: "100x100>" }, default_url: "/images/:style/missing.png"
validates_attachment_content_type :image, :content_type => ["image/jpg", "image/jpeg", "image/png", "image/gif"]
def all_tags=(names)
self.tags = names.split(",").map do |name|
Tag.where(name: name.strip).first_or_create!
end
end
def all_tags
self.tags.map(&:name).join(", ")
end
def self.tagged_with(name)
Tag.find_by_name!(name).posts
end
end
Schema
ActiveRecord::Schema.define(version: 20151026124712) do
create_table "posts", force: :cascade do |t|
t.string "title"
t.text "description"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.integer "user_id"
t.string "tags"
t.string "image_file_name"
t.string "image_content_type"
t.integer "image_file_size"
t.datetime "image_updated_at"
end
create_table "reviews", force: :cascade do |t|
t.text "comment"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.integer "user_id"
t.integer "post_id"
end
create_table "taggings", force: :cascade do |t|
t.integer "post_id"
t.integer "tag_id"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
add_index "taggings", ["post_id"], name: "index_taggings_on_post_id"
add_index "taggings", ["tag_id"], name: "index_taggings_on_tag_id"
create_table "tags", force: :cascade do |t|
t.string "name"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
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"
t.datetime "updated_at"
t.string "name"
end
add_index "users", ["email"], name: "index_users_on_email", unique: true
add_index "users", ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true
create_table "votes", force: :cascade do |t|
t.integer "votable_id"
t.string "votable_type"
t.integer "voter_id"
t.string "voter_type"
t.boolean "vote_flag"
t.string "vote_scope"
t.integer "vote_weight"
t.datetime "created_at"
t.datetime "updated_at"
end
add_index "votes", ["votable_id", "votable_type", "vote_scope"], name: "index_votes_on_votable_id_and_votable_type_and_vote_scope"
add_index "votes", ["voter_id", "voter_type", "vote_scope"], name: "index_votes_on_voter_id_and_voter_type_and_vote_scope"
end
As the error log says, there are foreign keys related to posts in taggings table, so it does not allow you to delete posts.
I guess a post has many taggings, and a tagging belongs to a tag? In this case, you need to delete all taggings belong to the post you want to delete. The easiest way is adding dependent: :destroy to your post model like
# models/post.rb
has_many :taggings, dependent: :destroy
You have some records in Taggings table which referenced to Post record.
So you have several options -
has_many :taggings, dependent: :destroy
Or you can change yours migration:
add_foreign_key :taggins, :posts, on_delete: :cascade (you can add this in database migration)
Described here

has and belongs to many rails 4

When I create a new group in my application I want the logged in user(Devise) to be associated with automatically. More users can be added to the group later on aswell. One user can have many groups. One group can have many users.
How do I associate the logged in user on creation?
My group.rb:
class Group < ActiveRecord::Base
has_and_belongs_to_many :users
has_and_belongs_to_many :clients
end
groups_controller.rb (create)
def create
#group = Group.new(group_params)
#group.users.id = current_user.id
respond_to do |format|
if #group.save
format.html { redirect_to #group, notice: 'Group was successfully created.' }
format.json { render action: 'show', status: :created, location: #group }
else
format.html { render action: 'new' }
format.json { render json: #group.errors, status: :unprocessable_entity }
end
end
end
And group_params from groups_controller.rb
def group_params
params.require(:group).permit(:name, { :user_ids => [] },)
end
My schema.rb contains:
create_table "groups", force: true do |t|
t.string "name"
t.string "background"
t.integer "clocktype"
t.datetime "created_at"
t.datetime "updated_at"
end
create_table "groups_users", id: false, force: true do |t|
t.integer "group_id"
t.integer "user_id"
end
add_index "groups_users", ["group_id"], name: "index_groups_users_on_group_id"
add_index "groups_users", ["user_id"], name: "index_groups_users_on_user_id"
create_table "users", force: true do |t|
t.string "first_name"
t.string "last_name"
t.string "email"
t.string "password"
t.string "avatar"
t.datetime "created_at"
t.datetime "updated_at"
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
t.datetime "current_sign_in_at"
t.datetime "last_sign_in_at"
t.string "current_sign_in_ip"
t.string "last_sign_in_ip"
end
Following would add the current user to the created group:
# In controller after #group.save
#group.users << current_user
See the association method docs for more info.
#group.user_ids = [current_user.id]
Try this:-
#group = Group.new(group_params)
#group.users = User.where(:id => current_user.id)
#group.save

not able to get user edits articles in ruby on rails

I am trying to get user who edits article in blog. I am able to get user who create article but got stuck while getting user who edits it. I tried with <%= #aritcle_update_user.username unless #article.user.nil? %> putting it in articles/show.html.erb(basically I am trying to show editing user on show page of articles). I have put this
def update
#article = Article.find(params[:id])
if #article.update_attributes(params[:article])
flash.notice = "Article '#{#article.title}' Updated!"
redirect_to article_path(#article)
#article.user_id = current_user.id
else
render 'edit'
end
end
in update action of article. Here I tried with #article.user_id = current_user.id and used it in show page of article but It throws me
NoMethodError in Articles#show
Showing f:/kuta/billi/app/views/articles/show.html.erb where line #36 raised:
undefined method `username' for 2:Fixnum
error.
articles_controller.rb
class ArticlesController < ApplicationController
before_filter :is_user_admin, only: [:new, :create, :edit, :destroy]
before_filter :log_impression, :only=> [:show]
def is_user_admin
redirect_to(action: :index) unless current_user.try(:is_admin?)
return false
end
def log_impression
#article = Article.find(params[:id])
# this assumes you have a current_user method in your authentication system
#article.impressions.create(ip_address: request.remote_ip,user_id:current_user.id)
end
def index
#articles = Article.all(:order => "created_at DESC")
#article_titles = Article.first(10)
#tags = Tag.all
end
def show
#article = Article.find(params[:id])
#related_articles = Article.joins(:taggings).where('articles.id != ?', #article.id).where(taggings: { tag_id: #article.tag_ids })
#article_popular = Article.order('articles.impressions_count DESC').limit(5)
end
def new
#article = Article.new
end
def create
#article = Article.new(params[:article])
#article.user_id = current_user.id
if #article.save
flash[:success] = "article created!"
redirect_to article_path(#article)
else
render 'new'
end
end
def destroy
#article = Article.find(params[:id])
#article.destroy
redirect_to action: 'index'
end
def edit
#article = Article.find(params[:id])
end
def update
#article = Article.find(params[:id])
if #article.update_attributes(params[:article])
flash.notice = "Article '#{#article.title}' Updated!"
redirect_to article_path(#article)
else
render 'edit'
end
end
end
articles/show.html.erb
<div style="margin-top:20px;margin-left:-10px""> <li> edit by<%= #aritcle_update_user.username unless #article.user.nil? %> </div>
schema.rb
ActiveRecord::Schema.define(:version => 20130421123420) do
create_table "articles", :force => true do |t|
t.string "title"
t.text "body"
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
t.integer "user_id"
t.integer "impressions_count"
end
create_table "comments", :force => true do |t|
t.text "content"
t.integer "user_id"
t.string "article_id"
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
end
create_table "impressions", :force => true do |t|
t.string "impressionable_type"
t.integer "impressionable_id"
t.integer "user_id"
t.string "ip_address"
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
end
create_table "taggings", :force => true do |t|
t.integer "tag_id"
t.integer "article_id"
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
end
add_index "taggings", ["article_id"], :name => "index_taggings_on_article_id"
add_index "taggings", ["tag_id"], :name => "index_taggings_on_tag_id"
create_table "tags", :force => true do |t|
t.string "name"
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
end
create_table "users", :force => true 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
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.boolean "is_admin"
t.boolean "is_active"
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
t.string "username"
end
Please help me to get user who edits article.If you need more code to pasted, please let me know.
You are setting #article.user_id = current_user.id in ArticlesController#update but trying to access that value in view of ArticlesController#show. that will not work.
so, to see the value #article.user_id in show.html.erb, you need to set it in corresponding controller ArticlesController#show like this:
def show
#article.user_id = current_user.id
end
if it still doesn't work, do post your model relationships as well, along with full error message & steps to reproduce that error

Resources