Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/61.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/ant/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails RubyonRails——将用户和产品控制器链接在一起是不起作用的_Ruby On Rails - Fatal编程技术网

Ruby on rails RubyonRails——将用户和产品控制器链接在一起是不起作用的

Ruby on rails RubyonRails——将用户和产品控制器链接在一起是不起作用的,ruby-on-rails,Ruby On Rails,更新2 到目前为止,我所做的是rails生成迁移将用户参考添加到产品中 然后我做到了 类AddUserReferenceToProducts

更新2

到目前为止,我所做的是rails生成迁移将用户参考添加到产品中

然后我做到了

类AddUserReferenceToProducts 但我正在获取PG::DuplicateColumn:ERROR:关系“请求项”的“用户id”列已经存在

当我在视图中引用它(作为@products.user_id)时,它是空的。我试着创造一个新产品,但它不起作用。我应该做什么,因为我现在做不到,为了我现在的生活,想想现在该做什么

更新

我想这样做@products.users.username(如果有其他方法,请告诉我)来显示用户创建的产品的用户名

目前,我还没有修改下面概述的代码,但如果需要更多信息,请告诉我。另外,如果你能提供一个循序渐进的过程,我将不胜感激

Clyde提供的答案对我不起作用(未定义的方法“username”)

问题:尽管已在模型中定义了此关系,但如何将用户控制器与产品控制器链接?(一个用户可以创建许多产品,而一个产品属于一个用户)

我有一个网站,在那里用户可以创建产品。现在,这些用户也有了一个个人资料页面。我想知道,当用户创建产品时,我点击产品本身,它会显示是哪个用户创建的,当我点击用户名时,它会指向他们的个人资料页面

目前,唯一不起作用的是用户名没有出现在他们创建的产品上,路由和其他一切都在起作用

对于用户,相关代码:

class UsersController < ApplicationController
  def index
    @users = User.all
  end

  def show
    @users = User.find(params[:id])
  end
end
对于产品:

class ProductsController < ApplicationController
  def index
    @products = Product.all
  end

  def new
    @products = Product.new
  end

  def create
      params[:products][:user_id] = current_user.id
      @products = products.new(products_params)
  end

  def edit
    @products = Product.find(params[:id])
  end

  def update
    @products = Products.find(params[:id])
    @products.update_attributes!(products_params)
    flash[:notice] = "#{@products.name} has been succesfully updated."
    redirect_to show_my_products_path
  end

  def destroy
    @products = Product.find(params[:id])
    @products.destroy
    flash[:notice] = "Your product '#{@products.name}' has been deleted."
    redirect_to show_my_products_path
  end

  def show
    @products = Product.find(params[:id])
  end

end
undefined method `username' for nil:NilClass
对于ShowProducts页面,它基本上显示产品信息。我想补充的一点是这一行:

<%= link_to "#{user.username}",  users_show_path_url(user), class: 'link_to'   %>

我猜,我不太确定如何链接创建他们产品的用户,并在特定产品上显示他们的用户名。我尝试添加:

@users = User.all
@users = User.find(params[:id])
至展会产品控制器

此外,这是一个模型:

class User < ActiveRecord::Base
  has_many :products
end

class Product < ActiveRecord::Base
  belongs_to :user
end
class用户
我故意省略了一些代码并选择了最相关的代码,但是如果您需要更多的代码,请告诉我

我想要的是,当有人单击该产品时,他们可以看到是哪个用户创建的,并且可以看到他们的用户名,因此单击该用户名可以转到用户配置文件。产品和用户之间的唯一链接是产品表中的“用户id”。因此,在创建产品时,用户id将绑定到该项目

试试看:

   <%= link_to user_path(@product.user), title: @product.user.username do %>
     <%= @product.user.username %>
   <% end %>

要使用变量
user
,您需要分配它,我看不出在show product action中这是在哪里完成的

请尝试通过产品访问用户:

<%= link_to "#{@products.user.username}",  users_show_path_url(@products.user), class: 'link_to' %>


您还需要确保您正在查看的产品实际上有一个用户,否则将导致相同类型的错误。

首先,我将创建designe用户

rails generate devise User
然后我将创建一个任务

rails g scaffold Task name:string description:text user:references
然后我将修改任务控制器

class TasksController < ApplicationController
  before_action :set_task, only: [:show, :edit, :update, :destroy]
  before_action :authenticate_user!, except: [ :show, :index]
  # GET /tasks
  # GET /tasks.json
  def index
    @tasks = Task.all
  end

  # GET /tasks/1
  # GET /tasks/1.json
  def show
  end

  # GET /tasks/new
  def new
    @task = current_user.tasks.new
  end

  # GET /tasks/1/edit
  def edit
  end

  # POST /tasks
  # POST /tasks.json
  def create
    @task = current_user.tasks.new(task_params)

    respond_to do |format|
      if @task.save
        format.html { redirect_to @task, notice: 'Task was successfully created.' }
        format.json { render :show, status: :created, location: @task }
      else
        format.html { render :new }
        format.json { render json: @task.errors, status: :unprocessable_entity }
      end
    end
  end

  # PATCH/PUT /tasks/1
  # PATCH/PUT /tasks/1.json
  def update
    respond_to do |format|
      if @task.update(task_params)
        format.html { redirect_to @task, notice: 'Task was successfully updated.' }
        format.json { render :show, status: :ok, location: @task }
      else
        format.html { render :edit }
        format.json { render json: @task.errors, status: :unprocessable_entity }
      end
    end
  end

  # DELETE /tasks/1
  # DELETE /tasks/1.json
  def destroy
    @task.destroy
    respond_to do |format|
      format.html { redirect_to tasks_url, notice: 'Task was successfully destroyed.' }
      format.json { head :no_content }
    end
  end

  private
    # Use callbacks to share common setup or constraints between actions.
    def set_task
      @task = Task.find(params[:id])
    end

    # Never trust parameters from the scary internet, only allow the white list through.
    def task_params
      params.require(:task).permit(:name, :description, :user_id)
    end
end
class ApplicationController < ActionController::Base
  before_action :configure_permitted_parameters, if: :devise_controller?

  protected

  def configure_permitted_parameters
    devise_parameter_sanitizer.permit(:sign_up, keys: [:username])
  end
end
然后我将运行migration命令

rake db:migrate
然后我将在注册表单#/views/devise/registrations/new.html.erb中添加用户名字段

<div class="field">
  <%= f.label :username %><br />
  <%= f.text_field :username, autofocus: true, autocomplete: "username" %>
</div>
然后,我将在用户模型中添加引用

has_many :tasks
现在,您可以在任务中显示您的用户名 随处可见

<%= task.user.username %>

下面是一个工作示例

我不确定产品控制器中的创建功能是否按预期工作

首先,在调用Product.new时,Product需要大写字母p。

其次,我认为您没有正确地关联用户

第三,需要将新实例保存到数据库中

根据您的产品参数,您可能会执行以下操作:

def create
  attributes = product_params.merge(user_id: current_user.id)
  @product = Product.new(attributes)

  if @product.save
    redirect_to show_my_products_path, notice: 'Product was successfully created.'
  else
    render :new
  end
end
在这种情况下,由于您没有在参数上传递user_id,因此您不需要产品参数中允许的user_id

然后在正确的视图中,您应该能够调用

<%= link_to @product.user.username, users_show_path_url(user), class: 'link_to' %> |

我没有所有的属性,因为这只是一个示例,但这为我生成了所有需要的代码,以使一切都按预期工作(除了模型中的has_许多)。这是一件非常有用的事情:)

嗨,猫,谢谢你。我刚刚完成了你的方法,我得到了nil:NilClass的未定义的方法“username”。根据您所说的,我是否也应该在产品控制器中定义用户?分配给
@products
的产品是否有用户?通过查看
@products.user
的结果,您可以非常轻松地进行检查。解决方案可以是通过验证强制关系,或者在呈现链接之前检查用户是否存在。
  validates :username, uniqueness: true, presence: true
has_many :tasks
<%= task.user.username %>
def create
  attributes = product_params.merge(user_id: current_user.id)
  @product = Product.new(attributes)

  if @product.save
    redirect_to show_my_products_path, notice: 'Product was successfully created.'
  else
    render :new
  end
end
<%= link_to @product.user.username, users_show_path_url(user), class: 'link_to' %> |
rails g scaffold user username

rails g scaffold products name user:references

rails db:migrate