形式参数不能是实例变量?

时间:2013-11-07 20:37:42

标签: ruby-on-rails

<% @idea.each do |@idea| %>
  <div id="basic_details" class="idea-show-columns">
    <%= render :partial => '/ideas/idea_basic_show', :locals => {:idea => @idea} %>
    <%= render :partial => 'ideas/comments' %>
    <%= render :partial => 'ideas/mockups' %>
  </div>

  <div id="copy_details" class="idea-show-columns">
    <%= render :partial => 'ideas/ copy_show', :locals => {:idea => @idea} %>
  </div>

<% end %>

当我关闭@时,它说“参数'想法'没有被使用”,我找不到合适的语法。

IdeasController

class IdeasController < ApplicationController

  def index
    respond_to do |format|
      if request.xhr?
        @ideas = Idea.select("sku, id").order(:sku)
      else
        if params[:search]
          @ideas = Idea.where("sku like '%#{params[:search]}%' or working_name like '%#
    {params[:search]}%' or product_name like '%#{params[:search]}%'").paginate(:page => 
    params[:page]).reload
        else
          @ideas = Idea.paginate(:page => params[:page]).reload
        end
      end
      format.html
      format.json {render json: @ideas.where("sku like ?", "%#{params[:q]}%") }
    end
  end

  def show
    @idea = Idea.find_by_permalink(params[:id])
  end

  def new
    @idea = Idea.new
  end

  def create
    @idea = Idea.new(new_idea_params)
    if @idea.save
      flash[:notice] = 'Idea created!'
      redirect_to ideas_url
    else
      render :new
    end
  end

  def edit
    @idea = Idea.find_by_permalink(params[:id])
  end

  def update
    @idea = Idea.find_by_permalink(params[:id])
    if request.xhr?
      if params[:comment]
        @idea.comments.create(:title => params[:comment][:title], :comment => params[:comment][:comment], :user_id => current_user.id)
      end
      if @idea.update_attributes(update_status_params)
        @success = true
      else
        @success = false
      end
    else
      if @idea.update_attributes(edit_idea_params)
        flash[:notice] = 'Idea updated!'
        redirect_to idea_url(params[:id])
      else
        render :edit
      end
    end
  end

  def destroy
    Idea.find_by_permalink(params[:id]).destroy
    flash[:success] = "Idea destroyed."
    redirect_to ideas_url
  end

  def generate_mockups
    idea = Idea.find_by_permalink(params[:id])
    begin
      idea.generate_mockups
    rescue Exception => e
      flash[:error] = "There was an error generating the mockups for #{idea.working_name}! # {e.message}"
    end
    flash[:notice] = "Successfully generated mockups for #{idea.working_name}"
    redirect_to idea_url(params[:id])
  end

  def signoff
    if current_user.has_overlord_access?
      if params[:idea_id]
        idea = Idea.find(params[:idea_id])
        idea.overlord_id = current_user.id
        idea.status = 'Ready To Publish'
        idea.save
        flash[:notice] = "#{current_user.full_name} just signed off on #{idea.sku}"
        redirect_to :action => :signoff
      end
      @ideas = Idea.awaiting_overlord_signoff.reload
      @idea = @ideas.first.reload
    else
      flash[:notice] = 'Sorry, you must have overlord access to sign off on ideas.'
      redirect_to ideas_path
    end
  end

  private

  def new_idea_params
    params.require(:idea).permit(:sku, :working_name, :working_description, :priority)
  end

  def update_status_params
    params.require(:idea).permit(:art_status, :copy_status)
  end

  def edit_idea_params
    (params[:idea][:color_ids] ||= []) unless !current_user.has_artist_access?
    (params[:idea][:imprintable_ids] ||= []) unless !current_user.has_copywriter_access?
    (params[:idea][:stores_ids] ||= []) unless !current_user.has_copywriter_access?
    (params[:idea][:taxonomies_ids] ||= []) unless !current_user.has_copywriter_access?

    params.require(:idea).permit(:sku, :working_name, :working_description, :priority,  
    :product_name, :product_line_tokens,
                                 :description, :meta_description, :meta_keywords, :artist_id, 
    :copywriter_id, :overlord_id,
                                  {:store_ids => []}, {:taxonomy_ids => []}, :base_price, 
    :shipping_category, :default_artwork_id,
                                  :tax_category, {:imprintable_ids => []}, :marketplace, 
    :product_type, 
                                  :base, :colors_offered, :special_instructions, :copy_status, 
    :art_status, {:color_ids => []}, 
                                  :artworks_attributes => [:height, :width, :from_top,    
    :from_center, :idea_id, :dimensions, :file, :_destroy, :id])
  end

  def update_status_params
    params.require(:idea).permit(:copy_status, :art_status)
  end
end

我收到错误

undefined method `each' for nil:NilClass

2 个答案:

答案 0 :(得分:3)

它应该:

<% unless @ideas.nil? %>

    <% @ideas.each do |idea| %>

        <div id="basic_details" class="idea-show-columns">
            <%= render :partial => '/ideas/idea_basic_show', :locals => {:idea => idea} %>
            <%= render :partial => 'ideas/comments' %>
            <%= render :partial => 'ideas/mockups' %>
        </div>

        <div id="copy_details" class="idea-show-columns">
            <%= render :partial => 'ideas/ copy_show', :locals => {:idea => idea} %>
        </div>

    <% end %>

<% end %>

答案 1 :(得分:0)

少数事情:

  • 如果您需要在show视图中使用@ideas,则需要使用before_filter
  • 加载它们
  • 不要这样做dea.where("sku like '%#{params[:search]}%'因为这不安全。做where("code = ?", params[:code])
  • 之类的事情