试图将部分表单添加到我的索引页面。如何传递多个变量来生成表单?
Trying to add a partial form to my index page. How do I pass multiple variables to generate the form?
我有一个艺术索引页,我使用评级模型对其进行了投票。我可以在显示页面上投票,但我希望它也能够在索引页面上投票。评级的形式具有三个变量。 Art Controller 索引页面应该如何显示以及索引 html 页面的代码。我收到 "rating" 未定义的错误。请帮忙,这是我完成这个项目必须克服的最后一个障碍。
**更新:我改变了路线,所以表格现在只有两个变量。但在索引页上呈现表单时仍然遇到问题。
通往新评级的路线
new_art_rating GET /arts/:art_id/ratings/new(.:format)
Routes.rb
resources :campaigns do
resources :lyrics do
member do
get "like", to: "lyrics#upvote"
get "dislike", to: "lyrics#downvote"
end
resources :comments
end
resources :arts do
member do
get "like", to: "arts#upvote"
get "dislike", to: "arts#downvote"
end
resources :commentarts
end
end
resources :arts do
resources :ratings
end
艺术指数
<% @arts.each do |art| %>
<div class="container-md">
<%= render partial: 'ratings/form', :locals =>{:campaign => @campaign, :art => @art, :rating => @art.rating} %>
<%= link_to campaign_art_path(@campaign, art) do %>
<%= image_tag art.image.url(:small) %>
<% end %>
艺术总监
class ArtsController < ApplicationController
before_action :tag_cloud, :only => [:index]
load_and_authorize_resource :only => [:show, :edit, :update, :destroy]
before_action :find_post, only: [:show, :edit, :update, :destroy, :upvote, :downvote]
before_action :set_campaign
before_action :authenticate_user!, except: [:index, :show]
impressionist :actions=>[:show,:index], :unique => [:impressionable_type, :impressionable_id, :session_hash]
def index
@arts = Art.all.order("created_at DESC").paginate(:page => params[:page], :per_page => 10)
@rating = Rating.new
end
def show
@commentarts = Commentart.where(art_id: @art)
@ratings = Rating.where(art_id: @art)
@random_art = Art.where.not(id: @art).order("RANDOM()").first
end
def new
@art = Art.new
end
def create
@art = Art.new(post_params)
@art.user_id = current_user.id
@art.campaign_id = @campaign.id
if @art.save
redirect_to([@art.campaign, @art])
else
render 'new'
end
end
def edit
end
def update
if @art.update(post_params)
redirect_to([@art.campaign, @art])
else
render 'edit'
end
end
def destroy
@art.destroy
redirect_to root_path
end
def upvote
@art.upvote_by current_user
redirect_to([@art.campaign, @art])
end
def downvote
@art.downvote_by current_user
redirect_to([@art.campaign, @art])
end
def tagged
if params[:tag].present?
@arts = Art.tagged_with(params[:tag]).order("created_at desc")
else
@arts = Art.all.order("created_at desc")
end
end
def tag_cloud
@tags = Art.tag_counts_on(:tags)
end
private
def find_post
@art = Art.find(params[:id])
end
def set_campaign
@campaign = Campaign.find(params[:campaign_id])
end
def post_params
params.require(:art).permit( :image, :description, :artist, :typeart, :link)
end
end
评分控制器
class RatingsController < ApplicationController
before_action :authenticate_user!
def create
@art = Art.find(params[:art_id])
@rating = Rating.create(params[:rating].permit(:rate))
@rating.user_id = current_user.id
@rating.art_id = @art.id
if @rating.save
redirect_to([@art.campaign, @art])
else
render 'new'
end
end
end
表单视图
<%= simple_form_for([@art, @art.ratings.build]) do |f| %>
<%= f.error_notification %>
<div class="field">
<div id="star-rating"></div>
</div>
<%= f.button :submit, "Rate", class: "button" %>
<% end %>
<script>
$('#star-rating').raty({
path: '/assets/',
scoreName: 'rating[rate]'
});
</script>
活动控制器
class CampaignsController < ApplicationController
before_action :find_campaign, only: [:show, :edit, :update, :destroy]
before_action :authenticate_user!, except: [:index, :show]
has_scope :free
has_scope :open
has_scope :closed
def index
@campaigns = Campaign.all.order("created_at DESC")
@free_campaign = Campaign.free.first
@open_campaigns = Campaign.open.all
@closed_campaigns = Campaign.closed.all
end
def show
@lyrics = Lyric.where(campaign_id: @campaign.id).order("created_at DESC")
@arts = Art.where(campaign_id: @campaign.id).order("created_at DESC")
@time = Time.now
end
def new
@campaign = current_user.campaigns.build
end
def create
@campaign = current_user.campaigns.build(campaign_params)
if @campaign.save
redirect_to @campaign
else
render 'new'
end
end
def edit
end
def update
if @campaign.update(campaign_params)
redirect_to @campaign
else
render 'edit'
end
end
def destroy
@campaign.destroy
redirect_to root_path
end
private
def find_campaign
@campaign = Campaign.find(params[:id])
end
def campaign_params
params.require(:campaign).permit(:title, :description, :timer, :timer2, :image, :status)
end
end
错误消息在 Arts#index 中。未定义的方法评级。我表格的第一行。
Arts 中的 NoMethodError#index nil:NilClass
的未定义方法“评分”
提取的源代码(第 1 行附近):
**<%= simple_form_for([@art, @art.ratings.build]) do |f| %>**
<%= f.error_notification %>
<div class="field">
<div id="star-rating"></div>
推测 Arts 索引有多个 Art 实例,所以没有 '@art'。
<% @arts.each do |art| %>
<%= simple_form_for([art, art.ratings.build]) do |f| %>
等或者,如果确实只有一个 Art 显示在 index 上,则在控制器中实例化它。
或许也编辑这一行?我需要查看错误日志以了解您在哪里看到错误。
Rating.create(params[:rating].permit(:rate))
我会把它分解成一个单独的方法:
Rating.create(rating_params[:rate))
def rating_params
params.require(:rating).permit(:rate)
end
然后我会检查 javascript 'raty' 库实际插入到 DOM 中的内容,以及它是否真的在发送您认为的参数?
我有一个艺术索引页,我使用评级模型对其进行了投票。我可以在显示页面上投票,但我希望它也能够在索引页面上投票。评级的形式具有三个变量。 Art Controller 索引页面应该如何显示以及索引 html 页面的代码。我收到 "rating" 未定义的错误。请帮忙,这是我完成这个项目必须克服的最后一个障碍。
**更新:我改变了路线,所以表格现在只有两个变量。但在索引页上呈现表单时仍然遇到问题。
通往新评级的路线
new_art_rating GET /arts/:art_id/ratings/new(.:format)
Routes.rb
resources :campaigns do
resources :lyrics do
member do
get "like", to: "lyrics#upvote"
get "dislike", to: "lyrics#downvote"
end
resources :comments
end
resources :arts do
member do
get "like", to: "arts#upvote"
get "dislike", to: "arts#downvote"
end
resources :commentarts
end
end
resources :arts do
resources :ratings
end
艺术指数
<% @arts.each do |art| %>
<div class="container-md">
<%= render partial: 'ratings/form', :locals =>{:campaign => @campaign, :art => @art, :rating => @art.rating} %>
<%= link_to campaign_art_path(@campaign, art) do %>
<%= image_tag art.image.url(:small) %>
<% end %>
艺术总监
class ArtsController < ApplicationController
before_action :tag_cloud, :only => [:index]
load_and_authorize_resource :only => [:show, :edit, :update, :destroy]
before_action :find_post, only: [:show, :edit, :update, :destroy, :upvote, :downvote]
before_action :set_campaign
before_action :authenticate_user!, except: [:index, :show]
impressionist :actions=>[:show,:index], :unique => [:impressionable_type, :impressionable_id, :session_hash]
def index
@arts = Art.all.order("created_at DESC").paginate(:page => params[:page], :per_page => 10)
@rating = Rating.new
end
def show
@commentarts = Commentart.where(art_id: @art)
@ratings = Rating.where(art_id: @art)
@random_art = Art.where.not(id: @art).order("RANDOM()").first
end
def new
@art = Art.new
end
def create
@art = Art.new(post_params)
@art.user_id = current_user.id
@art.campaign_id = @campaign.id
if @art.save
redirect_to([@art.campaign, @art])
else
render 'new'
end
end
def edit
end
def update
if @art.update(post_params)
redirect_to([@art.campaign, @art])
else
render 'edit'
end
end
def destroy
@art.destroy
redirect_to root_path
end
def upvote
@art.upvote_by current_user
redirect_to([@art.campaign, @art])
end
def downvote
@art.downvote_by current_user
redirect_to([@art.campaign, @art])
end
def tagged
if params[:tag].present?
@arts = Art.tagged_with(params[:tag]).order("created_at desc")
else
@arts = Art.all.order("created_at desc")
end
end
def tag_cloud
@tags = Art.tag_counts_on(:tags)
end
private
def find_post
@art = Art.find(params[:id])
end
def set_campaign
@campaign = Campaign.find(params[:campaign_id])
end
def post_params
params.require(:art).permit( :image, :description, :artist, :typeart, :link)
end
end
评分控制器
class RatingsController < ApplicationController
before_action :authenticate_user!
def create
@art = Art.find(params[:art_id])
@rating = Rating.create(params[:rating].permit(:rate))
@rating.user_id = current_user.id
@rating.art_id = @art.id
if @rating.save
redirect_to([@art.campaign, @art])
else
render 'new'
end
end
end
表单视图
<%= simple_form_for([@art, @art.ratings.build]) do |f| %>
<%= f.error_notification %>
<div class="field">
<div id="star-rating"></div>
</div>
<%= f.button :submit, "Rate", class: "button" %>
<% end %>
<script>
$('#star-rating').raty({
path: '/assets/',
scoreName: 'rating[rate]'
});
</script>
活动控制器
class CampaignsController < ApplicationController
before_action :find_campaign, only: [:show, :edit, :update, :destroy]
before_action :authenticate_user!, except: [:index, :show]
has_scope :free
has_scope :open
has_scope :closed
def index
@campaigns = Campaign.all.order("created_at DESC")
@free_campaign = Campaign.free.first
@open_campaigns = Campaign.open.all
@closed_campaigns = Campaign.closed.all
end
def show
@lyrics = Lyric.where(campaign_id: @campaign.id).order("created_at DESC")
@arts = Art.where(campaign_id: @campaign.id).order("created_at DESC")
@time = Time.now
end
def new
@campaign = current_user.campaigns.build
end
def create
@campaign = current_user.campaigns.build(campaign_params)
if @campaign.save
redirect_to @campaign
else
render 'new'
end
end
def edit
end
def update
if @campaign.update(campaign_params)
redirect_to @campaign
else
render 'edit'
end
end
def destroy
@campaign.destroy
redirect_to root_path
end
private
def find_campaign
@campaign = Campaign.find(params[:id])
end
def campaign_params
params.require(:campaign).permit(:title, :description, :timer, :timer2, :image, :status)
end
end
错误消息在 Arts#index 中。未定义的方法评级。我表格的第一行。
Arts 中的 NoMethodError#index nil:NilClass
的未定义方法“评分”提取的源代码(第 1 行附近):
**<%= simple_form_for([@art, @art.ratings.build]) do |f| %>**
<%= f.error_notification %>
<div class="field">
<div id="star-rating"></div>
推测 Arts 索引有多个 Art 实例,所以没有 '@art'。
<% @arts.each do |art| %>
<%= simple_form_for([art, art.ratings.build]) do |f| %>
等或者,如果确实只有一个 Art 显示在 index 上,则在控制器中实例化它。
或许也编辑这一行?我需要查看错误日志以了解您在哪里看到错误。
Rating.create(params[:rating].permit(:rate))
我会把它分解成一个单独的方法:
Rating.create(rating_params[:rate))
def rating_params
params.require(:rating).permit(:rate)
end
然后我会检查 javascript 'raty' 库实际插入到 DOM 中的内容,以及它是否真的在发送您认为的参数?