简体   繁体   English

Ruby on Rails:User_id和category_id为nil

[英]Ruby on Rails: User_id and category_id is nil

Following to my last question ( Ruby on Rails: Relation between two models ) I've got some new problems: 接下来是我的最后一个问题( Ruby on Rails:两个模型之间的关系 ),我遇到了一些新问题:

I am using Devise and there you can enter a user name beside email and so on. 我正在使用Devise,您可以在其中输入电子邮件旁边的用户名,等等。 This works and the name gets saved to the db. 这有效,并且名称被保存到数据库。

Now I'm trying to call that user name on a specific "show" page, to illustrate who created the request: 现在,我试图在特定的“显示”页面上调用该用户名,以说明谁创建了请求:

<%= @request.user.name if @request.user %>

Furthermore I want only the specific user who created the request to be able to delete and edit: 此外,我只希望创建请求的特定用户能够删除和编辑:

<% if current_user && @request.user == current_user %>
   <%= link_to edit_request_path(@request), class: "btn btn-default" do %>
   <span class="glyphicon glyphicon-edit"></span>
       Edit
   <% end %>
   <%= link_to @request, method: :delete, data: { confirm: 'Bist du sicher?' }, class: "btn btn-default" do %>
   <span class="glyphicon glyphicon-trash"></span>
       Löschen
   <% end %>
<% end %>

For that I've set up a class in my requests_controller.rb: 为此,我在我的request_controller.rb中设置了一个类:

def correct_user
  @request = current_user.requests.find_by(id: params[:id])
  redirect_to requests_path, alert: 'Keine Berechtigung bearbeiten' if @request.nil?
end

But neither the username shows up nor the delete and edit buttons :( 但是,既不会显示用户名,也不会显示删除和编辑按钮:(

Here is the request.rb: 这是request.rb:

class Request < ActiveRecord::Base
 belongs_to :category
 belongs_to :user
 validates :description, presence: true, length: { maximum: 500 }
 validates :title, presence: true
 validates :budget, presence: true
 validates :category, presence: true
end

and the user.rb: 和user.rb:

class User < ActiveRecord::Base
  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :trackable, :validatable
         has_many :requests
         validates :name, presence: true
end

I did this migration to add the UserID to Requests model so I can call the user through @request.user.name: 我进行了此迁移,以将UserID添加到Requests模型,以便可以通过@ request.user.name调用用户:

class AddUserIdToRequests < ActiveRecord::Migration
  def change
    add_column :requests, :user_id, :integer
  end
end

excerpt of my request_controller.rb: 我的request_controller.rb的摘录:

class RequestsController < ApplicationController
before_action :correct_user, only: [:edit, :update, :destroy]

private
    def set_request
      @request = Request.find(params[:id])
    end

    def correct_user
      @request = current_user.requests.find_by(id: params[:id])
      redirect_to requests_path, alert: 'Keine Berechtigung bearbeiten' if @request.nil?
    end

    def request_params
      params.require(:request).permit(:title, :description, :tags, :budget, :name)
    end

Make sure the method correct_user is called before you render the view. 在渲染视图之前,请确保调用了correct_user方法。 On second note, make sure the request object has a user associated with it.(ie, @request.user should not be nil) 第二点,请确保request对象具有与之关联的user 。(即,@ request.user不应为nil)

Also the way you compare the logged in user and owner is wrong at below line 另外,在下面的行中,您比较登录用户和所有者的方式是错误的

<% if current_user && @request.user == current_user %>

It should be something like this 应该是这样的

<% if current_user && @request.user.id == current_user.id %>

If you compare objects, they wont be same. 如果您比较对象,它们将不会相同。 Please compare id 请比较id

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM