current_user.id返回文章创建者的id

时间:2015-06-23 18:39:28

标签: ruby-on-rails ruby devise

我正在使用设计current_user来显示edit按钮,具体取决于current_user是否与创建user_id的{​​{1}}匹配。

这在我以前从未发生过,我已经尝试过搜索此错误但找不到任何内容,也许这是我做错了,我无法弄明白。

无论如何,这是我的文件:

宝石:

Article

article_controller:

source 'https://rubygems.org'


# Bundle edge Rails instead: gem 'rails', github: 'rails/rails'
gem 'rails', '4.2.1'
# Use postgresql as the database for Active Record
gem 'pg'
# Use SCSS for stylesheets
gem 'sass-rails', '~> 5.0'
# Use Uglifier as compressor for JavaScript assets
gem 'uglifier', '>= 1.3.0'
# Use CoffeeScript for .coffee assets and views
gem 'coffee-rails', '~> 4.1.0'
# See https://github.com/rails/execjs#readme for more supported runtimes
# gem 'therubyracer', platforms: :ruby

# Use jquery as the JavaScript library
gem 'jquery-rails'
# Turbolinks makes following links in your web application faster. Read more: https://github.com/rails/turbolinks
gem 'turbolinks'
gem 'jquery-turbolinks'
# Build JSON APIs with ease. Read more: https://github.com/rails/jbuilder
gem 'jbuilder', '~> 2.0'
# bundle exec rake doc:rails generates the API under doc/api.
gem 'sdoc', '~> 0.4.0', group: :doc

# Use ActiveModel has_secure_password
# gem 'bcrypt', '~> 3.1.7'

# Use Unicorn as the app server
# gem 'unicorn'

# Use Capistrano for deployment
# gem 'capistrano-rails', group: :development

gem 'bootstrap-sass', '~> 3.3.5'

gem 'evil_icons'

gem 'devise'

gem 'ransack'

gem "will_paginate"

group :development, :test do
  # Call 'byebug' anywhere in the code to stop execution and get a debugger console
  gem 'byebug'

  # Access an IRB console on exception pages or by using <%= console %> in views
  gem 'web-console', '~> 2.0'

  # Spring speeds up development by keeping your application running in the background. Read more: https://github.com/rails/spring
  gem 'spring'

  gem 'better_errors'

  gem 'faker'

  gem 'nifty-generators'
end

示例:

注意:要进入class ArticlesController < ApplicationController before_action :set_article, only: [:show, :edit, :update, :destroy] before_action :authenticate_user!, :except => [:show, :index] # before_action :redirect_coming_soon # before_filter :require_permission, only: :edit # GET /articles # GET /articles.json def index @q = Article.ransack(params[:q]) @articles = @q.result.paginate(page: params[:page], :per_page => 10) end # GET /articles/1 # GET /articles/1.json def show end # GET /articles/new def new @article = Article.new end # GET /articles/1/edit def edit end # POST /articles # POST /articles.json def create @article = current_user.articles.build(article_params) 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 # PATCH/PUT /articles/1 # PATCH/PUT /articles/1.json def update respond_to do |format| if @article.update(article_params) format.html { redirect_to @article, notice: 'Article was successfully updated.' } format.json { render :show, status: :ok, location: @article } else format.html { render :edit } format.json { render json: @article.errors, status: :unprocessable_entity } end end end # DELETE /articles/1 # DELETE /articles/1.json def destroy @article.destroy respond_to do |format| format.html { redirect_to articles_url, notice: 'Article was successfully destroyed.' } format.json { head :no_content } end end private # Use callbacks to share common setup or constraints between actions. def set_article @article = Article.find(params[:id]) end # Never trust parameters from the scary internet, only allow the white list through. def article_params params.require(:article).permit(:name, :price, :description, :category_id) end def require_permission if current_user != Article.find(params[:id]).user redirect_to root_path #Or do something else here end end end 控制台,我只需要放入我想要的视图better_errors

我有2位用户注册

<%aa%> user_id: 1, email: '1@test.com'

如果我使用#2用户创建user_id: 2, email: '2@test.com'并进入网址Article并输入控制台localhost:3000/articles/1,则会显示current_user

但是,如果我退出该用户并使用#1登录并转到同一地址并在控制台user_id: 2中输入,它将为我提供current_user,但其他所有内容都会正确返回(电子邮件等)

就像它返回创建文章的人的ID,而不是返回user_id: 2

此外,如果我进入视图并添加

current_user.id

它应该正常工作,它返回登录用户的id和电子邮件,但在控制台中没有,它返回创建文章的人的id。

enter image description here enter image description here

<p><%= current_user.id %></p>
<p><%= current_user.email %></p>

即使我使用不同的用户,这个链接也会在每篇文章中出现。

1 个答案:

答案 0 :(得分:0)

嗯,两个人的思想比一个人好(或者在这种情况下观察)。

视图中有拼写错误

而不是:

<% if current_user.id = @article.user_id %>
    <%= link_to 'Edit', edit_article_path(@article) %> |
<% else %>

更改为:

<% if current_user.id == @article.user_id %>
    <%= link_to 'Edit', edit_article_path(@article) %> |
<% else %>

双重==标志,这些标志让事情变得糟糕......