将用户ID和投票ID传递给评论

时间:2015-12-01 11:19:47

标签: ruby-on-rails ruby-on-rails-4

我是一名新的rails开发人员,我目前正在开发一个项目,用户可以在该项目中发布参数。 (或对帖子发表评论) 我有一个设计用户模型,一个投票模型和一个参数模型。

我的问题是我似乎无法访问用户属性,所以我可以在我的视图中将<%= argument.user.first_name %>添加到参数中。
我的问题是argument.user.first_name返回此错误:undefined method first_name'为nil:NilClass`

当我尝试<%= argument.vote.title %>时,它的效果非常好。

当我去控制台找到投票和参数时,argument.user.first_name我得到了正确的名字。

迁移看起来像这样:

 create_table "arguments", force: true do |t|
    t.string   "title"
    t.text     "argument"
    t.integer  "vote_id"
    t.integer  "user_id"
    t.datetime "created_at"
    t.datetime "updated_at"
  end

所以我拥有来自用户的外键和参数表中的投票。

我在投票和用户上添加了“has_many”关系。所以用户和投票都是has_many:arguments。

我的参数控制器看起来像这样:

    class ArgumentsController < ApplicationController
    before_filter :current_user

    def create
        @vote = Vote.find(params[:vote_id])
        @argument = @vote.arguments.build(params[:argument].permit(:argument))
        @argument.user_id = current_user.id

        @argument.save!

        if @argument.save
            redirect_to @vote
        else
            render 'new'
        end
    end

    private
        def argument_params
            params.require(:argument).permit(:title, :argument)

        end
end

我找到了带有id的投票并在该投票上创建了评论。 我试图传递user_id,所以我能够访问用户表,如图片,first_name等。

我的论点_form:

<div class="well clearfix">
                    <h2>Tilføj et argument</h2>


<%= form_for([@vote, @vote.arguments.build]) do |f| %>

                    <%= f.hidden_field :user_id, :value => @current_user.id %>

                    <p><%= f.label :Bruger %><br>
                    <%= current_user.first_name %>

                    <p><%= f.label :Titel %><br>
                    <%= f.text_field :title %></p>

                    <p><%= f.label :Argument %><br>
                    <%= f.text_area :argument %>

                    <p><%= f.submit 'Opret', class: 'btn btn-primary' %></p>
                    <% end %>

                </div>

我使用vote_id构建参数 并使用_arguments显示它:

     <% @vote.arguments.each do |argument| %>
    <%= div_for(argument, class: 'col-md-4 well clearfix') do |argument| %>

              <h2><%= argument.title %></h2>


              <%= argument.argument %>
            <p>Af:
            <%= argument.user_id %>

            <%= argument.user.first_name %>

    <% end %>
<% end %>

对我来说,似乎没有使用create argument方法正确传递user_id?

在我的路线中,我将参数作为投票的嵌套资源:

    devise_scope :user do 
    resources :users, only: [:show]
  end

    root 'votes#index'
      resources :votes do
        resources :arguments
      end

希望你们中的一位能帮助我,引导我朝着正确的方向前进! 在此先感谢:)

3 个答案:

答案 0 :(得分:2)

您需要在控制器中对用户进行身份验证,例如: -

 before_filter :authenticate_user!

更改参数控制器: -

class ArgumentsController < ApplicationController
    before_filter :authenticate_user!

    def create
        @vote = Vote.find(params[:vote_id])
        @argument = @vote.arguments.build(argument_params)
        if @argument.save
            redirect_to @vote
        else
            render 'new'
        end
    end

    private
    def argument_params
        params.require(:argument).permit(:title, :argument, :user_id)
    end
end

参数形式将@current_user更改为current_user:

<%= f.hidden_field :user_id, :value => current_user.id %>

此外,您不需要在隐藏字段中发送用户ID,因为如果用户登录,我们可以在控制器中获取当前用户,然后您可以在控制器中为参数分配user_id,如: -

@argument.user_id = current_user.id

答案 1 :(得分:0)

由于您的argument模型有belongs_to :user,因此您可以指定@argument.user = current_user而不是ID。由于current_user已经加载,因此开销很小,只有argument无法重新加载

与问题无关,但需要修复:

  1. save / save!的两次调用很奇怪,通常只需要一次,特别是在代码中稍后检查保存是否成功时(现在您将在验证错误时遇到异常)形式渲染)
  2. argument_params来电被params[:argument].permit(:argument)替换(如果您遇到没有nil的请求,则会产生关于argument的错误,而不是必需的参数。
  3. 无论如何,
  4. user_id隐藏字段被强参数丢弃,最好将其删除(并且不允许可能的冒名顶替者)

答案 2 :(得分:0)

啊,啊,终于来了!我设法解决了自己的愚蠢问题。

探测是我在参数之前渲染表单(创建参数)。这创建了一个空参数div / object,它不包含任何内容。所以错误总是在那个空对象上(obv确实包含了用户信息,因为它是空的)

所以你的答案是正确的,但我的问题是错的,哈哈 好的,谢谢你的时间!

在我的show.html中,我添加了

        <%= render 'arguments/argument' %>
<div class="jumbotron">
    <%= render 'arguments/form' %>
</div>
相关问题