2

我已经尝试解决这个问题几天了,但没有运气。

我正在开发一个包含 4 个模型的 webapp;用户、投票、论据和评级。评级模型应该负责对参数进行评级。

这些协会是:

class Rating < ActiveRecord::Base
  belongs_to :user
  belongs_to :argument
end

class Vote < ActiveRecord::Base
    has_many :vote_options, dependent: :destroy
    has_many :arguments
    accepts_nested_attributes_for :vote_options, :reject_if => :all_blank, :allow_destroy => true

class Argument < ActiveRecord::Base
  belongs_to :user
  belongs_to :vote
  has_many :ratings

class User < ActiveRecord::Base

  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :trackable, :validatable, 
         :omniauthable, :omniauth_providers => [:facebook]
  has_many :uservotes, dependent: :destroy
  has_many :vote_options, through: :uservotes
  has_many :arguments, through: :votes
  has_many :ratings

我的问题是我试图访问评级视图中的@argument,以便传递正在评级的当前参数的argument.id。

    _argument.html.erb

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

                    <div class="col-md-12">
                        <h2><%= argument.title %></h2>
                    </div>

                    <div class="col-md-8">
                        <p><%= argument.argument %></p>
                    </div>

                    <div class="col-md-4">
                        <div class="col-md-12">
                            <%= image_tag argument.user.picture, class: 'img-responsive' %>
                        </div>
                        <div class="col-md-12">
                            <p>Af: <%= argument.user.first_name %></p>
                        </div>
                    </div>
                    <div class="col-md-12">
                        <%= render 'ratings/rating' %>
                    </div>

    <% end %>

                </div>
            </div>
<% end %>

<% end %>

这工作正常,但部分即时渲染(评级)不会采用@argument 实例变量。

_rating.html.erb

<%= form_for ([@argument, @argument.ratings.new]), as: :post, url: new_rating_path(@argument), method: :patch, html: { class: "edit_post", id: "edit_post_45" } do |f| %>

  <fieldset class="rating">
    <h1><%= @argument.id %></h1>

    <%= f.radio_button :score, '5', class: 'star', id: "star5_#{@argument.id}", value: 5 %>
    <%= f.label 'a', class: "full", for: "star5_#{@argument.id}" %>

    <%= f.radio_button :score, '4', class: 'star', id: "star4_#{@argument.id}", value: 4 %>
    <%= f.label '', class: "full", for: "star4_#{@argument.id}" %>

    <%= f.radio_button :score, '3', class: 'star', id: "star3_#{@argument.id}", value: 3 %>
    <%= f.label '', class: "full", for: "star3_#{@argument.id}" %>

    <%= f.radio_button :score, '2', class: 'star', id: "star2_#{@argument.id}", value: 2 %>
    <%= f.label '', class: "full", for: "star2_#{@argument.id}" %>

    <%= f.radio_button :score, '1', class: 'star', id: "star1_#{@argument.id}", value: 1 %>
    <%= f.label '', class: "full", for: "star1_#{@argument.id}" %>



  </fieldset>
  <div class="actions">
    <%= f.submit 'Rate', class: 'btn btn-primary' %>
  </div>
<% end %>

我想将它放在 form_for 中,并能够使用 css 类的字符串插值来访问它。

我做了一些事情来解决这个问题:我在投票控制器中添加了一个 before_action,我在其中设置了参数。这使得访问 @argument 实例变量成为可能,但它似乎只接受了第一个参数,所以它们都有相同的 ID。

def set_argument
  @argument = Argument.find_by(params[:id])
end

如何在表单中包含 @argument 实例变量?将 argument.id 和 user.id 传递给评级模型?当前代码给了我这个错误: undefined method `humanize' for nil:NilClass

另外,在提交评分表时,会出现路线错误,我不知道为什么会出现。

我的路线文件如下所示:

Rails.application.routes.draw do

  devise_for :users, controllers: { omniauth_callbacks: "users/omniauth_callbacks", registrations: 'users' }
  devise_scope :user do 
    resources :users, only: [:show]
  end

  root 'votes#index'
  resources :votes do
    resources :arguments
  end
  resources :uservotes, only: [:create]
  resources :ratings

我希望有人能够帮助我(或指导我正确的方向)提前谢谢!

为投票添加参数的 _form:

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

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

<p><%= f.label :Titel %><br>
<%= f.text_field :title, placeholder: "Skriv en overskrift" %></p>
<p><%= f.label :Argument %><br>
<%= f.text_area :argument, cols: 35, rows: 6, placeholder: "Max 250 tegn" %>

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

2 回答 2

0

您实际上是在传递@vote对象,并且参数是通过 ActiveRecord 关联附加的。这是如何传递参数...

<%= render 'ratings/rating', vote: @vote  %>

然后,如果您需要将参数传递给另一个部分...

# no '@' since you got it from votes.
<%= render 'ratings/rating', argument: argument %>

然后失去你@的部分争论......

<%= form_for ([argument, argument.ratings.new]), as: :post, url: new_rating_path(argument), method: :patch, html: { class: "edit_post", id: "edit_post_45" } do |f| %>
于 2015-12-08T17:06:06.007 回答
0

您应该将参数作为本地参数传递给 _argument.html.erb 中的渲染部分:

<%= render 'ratings/rating', locals: { argument: argument }  %>

那么您的 _rating.html.erb 部分将具有:

<%= form_for ([argument, argument.ratings.new]), as: :post, url: new_rating_path(argument), method: :patch, html: { class: "edit_post", id: "edit_post_45" } do |f| %>

  <fieldset class="rating">
    <h1><%= argument.id %></h1>

    <%= f.radio_button :score, '5', class: 'star', id: "star5_#{argument.id}", value: 5 %>
    <%= f.label 'a', class: "full", for: "star5_#{argument.id}" %>

    <%= f.radio_button :score, '4', class: 'star', id: "star4_#{argument.id}", value: 4 %>
    <%= f.label '', class: "full", for: "star4_#{argument.id}" %>

    <%= f.radio_button :score, '3', class: 'star', id: "star3_#{argument.id}", value: 3 %>
    <%= f.label '', class: "full", for: "star3_#{argument.id}" %>

    <%= f.radio_button :score, '2', class: 'star', id: "star2_#{argument.id}", value: 2 %>
    <%= f.label '', class: "full", for: "star2_#{argument.id}" %>

    <%= f.radio_button :score, '1', class: 'star', id: "star1_#{argument.id}", value: 1 %>
    <%= f.label '', class: "full", for: "star1_#{argument.id}" %>



  </fieldset>
  <div class="actions">
    <%= f.submit 'Rate', class: 'btn btn-primary' %>
  </div>
<% end %>

编辑:因为它是渲染的唯一参数,你可能只是这样做:

<%= render 'ratings/rating', argument: argument  %>
于 2015-12-08T16:07:47.780 回答