尝试将部分表单添加到我的索引页面。我如何传递多个变量来生成表单? [英] Trying to add a partial form to my index page. How do I pass multiple variables to generate the form?

查看:86
本文介绍了尝试将部分表单添加到我的索引页面。我如何传递多个变量来生成表单?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个艺术指数页面,可以用评分模型进行投票。我可以在节目页面投票,但我希望它能够在索引页面上进行投票。评级的形式有三个变量。 Art Controller索引页面应该如何查看以及index html页面的代码。我收到评分未定义的错误。请帮助,这就像是我完成这个项目的最后一道障碍。



**更新:我改变了我的路线,所以表格现在只有两个变量。但仍然无法在索引页上呈现表单。



路由到新的评分



new_art_rating GET / art /:art_id / ratings / new(。:format)

Routes.rb

  resources:campaigns do 
resources:lyrics do
member do
getlike,to:lyrics#upvote
getdislike,to: lyrics#downvote
结束

资源:评论
结束

资源:arts do
成员do
获得喜欢,改为:arts#upvote
得到不喜欢,改为:arts#downvote
end

资源:commentarts


end
end
资源:arts do
资源:rating
end



艺术指数

 <%@ arts.each do | art | %GT; 
< div class =container-md>
<%=呈现部分:'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 !,除了:[:index::show]
印象派:动作=> [: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
渲染'新'
结束

结束

def编辑
结束

def更新
if @ art.update(post_params)
redirect_to([ @ art.campaign,@art])
else
渲染'编辑'
结束

结束

def破坏
@ art.destroy
redirect_to root_path
end

def upvote
@ art.upvote_by current_user
redirect_to([@ art.campaign,@art])
end
$ b $ def downvote
@ art.downvote_by current_user
redirect_to([@ art.campaign,@art])
end

def标记
如果params [:tag] .present?
@arts = Art.tagged_with(params [:tag])。order(created_at desc)
else
@arts = Art.all.order(created_at desc)
结束
结束

def tag_cloud

@tags = Art.tag_counts_on(:tags)
结束


私人

def find_post
@art = Art.find(params [:id])
结束

def set_campaign
@campaign = Campaign.find(params [:campaign_id])
end
$ b def post_params
params.require(:art).permit(:image,:description,:artist ,:typeart,:link)
结束


结束



评级控制器

  class RatingsController< / p> ApplicationController 
before_action:authenticate_user!
$ b $ def创建
@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 | %GT; 
<%= 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,只有:[:show,:edit,:update,:destroy]
before_action:authenticate_user !,除外:[:index,:show]
has_scope:free
has_scope:打开
has_scope:关闭

def索引
@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
渲染'新'
结束
$ b $结束

def编辑
结束

def更新
if @ campaign.update(campaign_params)
redirect_to @campaign
else
渲染'编辑'
结束

end

def destroy
@ campaign.destroy
redirect_to root_path
end

$ b private

:def find_campaign
@campaign = Campaign.find(params [:id])
结束

def campaign_params
params.require(:campaign).permit(: title,:description,:timer,:timer2,:image,:status)
结束

结束

错误消息位于Arts#索引中。未定义的方法评级。第一行是我的表单。



Arts中的NoMethodError#index未定义的方法`rating'为nil:NilClass



提取的源代码(在第1行左右):

  **<%= simple_form_for([@ art,@ art.ratings。 build])do | f | %> ** 
<%= f.error_notification%>
< div class =field>
< div id =star-rating>< / div>


解决方案

据推测艺术指数有多个艺术实例,不是'@art'。

 <%@ arts.each do | art | %GT; 
<%= simple_form_for([art,art​​.ratings.build])do | f | %GT;

等等。或者,如果索引中只显示一个Art,则在控制器中实例化。



也许还可以编辑此行?

  Rating.create(params [:rating])我们需要查看错误日志以查看发生错误的位置。 .permit(:rate))

我将其分解为一个单独的方法:

  Rating.create(rating_params [:rate))

def rating_params
params.require(:评级)。许可证(:比率)
结束

我会检查javascript 'raty'库实际上是插入到DOM中,它是否真的在发送你认为是的参数?


I have an index page of art that I vote on with ratings model. I can vote on the show page but I want it to be able to vote on the index page also. The form for the ratings has three variables. How should the Art Controller index page look and the code for the index html page. I'm getting an error that "rating" is undefined. Please help, this is like the last hurdle I got to get over to complete this project.

**Update: I changed my routes so the form only has two variables now. But still having trouble rendering the form on the index page.

Routes to a new 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

Art Index

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

Arts Controller

    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

Ratings Controller

    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

Form view

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

Campaign Controller

    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

Error Message is in the Arts#index. Undefined method ratings. First line in my form.

NoMethodError in Arts#index undefined method `ratings' for nil:NilClass

Extracted source (around line #1):

**<%= simple_form_for([@art, @art.ratings.build]) do |f| %>**
<%= f.error_notification %>
<div class="field">
<div id="star-rating"></div>

解决方案

Presumably Arts index has multiple Art instances, so there is not '@art'.

<% @arts.each do |art| %>
  <%= simple_form_for([art, art.ratings.build]) do |f| %>

Etc. Or if there really is only one Art displayed on index, then instantiate that in the controller.

Perhaps also edit this line? I need to see the error log to see where you're seeing the error.

Rating.create(params[:rating].permit(:rate))

I'd break that out into a separate method:

Rating.create(rating_params[:rate))

def rating_params
  params.require(:rating).permit(:rate)
end

And I'd check what the javascript 'raty' library is actually inserting into the DOM and whether it really is sending the params you think it is?

这篇关于尝试将部分表单添加到我的索引页面。我如何传递多个变量来生成表单?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆