2015-12-08 113 views
0

我試圖解決這個問題一段時間了,但沒有運氣。將實例變量從另一個模型傳遞到表單

我正在研究一個包含4個模型的web應用程序;用戶,投票,論點和評級。評級模型是假設照顧參數的評分。

的關聯是:

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 %> 

這工作正常,但部分im渲染(評級)將不會採取@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,其中我set_argument。這使得訪問@argument實例變量成爲可能,但它似乎只是第一個參數,所以它們都具有相同的ID。

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

如何在表單中包含@argument實例變量?將參數.id和user.id傳遞給評分模型? 當前的代碼給了我這個錯誤:無 未定義的方法`人文化」: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 %> 

回答

0

實際上你傳入的是對象,而參數是通過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| %> 
+0

嗯,然後刪除form_for中的@argument? 我得到未定義的方法'人性化爲零:NilClass錯誤的<%= f.label',類:「全部」,爲:「star4_#{@argument.id}」%>當我只是通過參數參數輸入正確的代碼 –

+0

通過將參數添加到partial中,我得到的問題與我在before_action中執行set_argument時的結果相同:votes_controller中的set_argument。只有第一個argumetns id在那裏......並且它是在參數(@argument)的每個實例變量中爲評級視圖 –

+0

傳遞的。將@argument保留在form_for中。對不起, – Beengie

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 %> 
+0

我試過了,但我仍然得到未定義的局部變量或方法'參數'爲#<#:0x58911c0> 我不需要在控制器中設置@argument? –

相關問題