繁体   English   中英

尝试在视图中渲染simple_form时,为NilClass:Class引入未定义的方法“ model_name”

[英]Rails undefined method `model_name' for NilClass:Class When trying to render simple_form in a view

我已经搜索了所有问题,却找不到适合我的答案,而且我是Rails的新手,并且陷入困境。 我正在尝试渲染用于将状态发布到用户个人资料视图中的simple_form。 但是每次加载个人资料页面时,都会出现以下错误:

undefined method `model_name' for NilClass:Class

Extracted source (around line #1):

1: <%= simple_form_for(@status) do |f| %>

我不知道为什么,因为它在我拥有的另外两个视图中可以正常显示,只是区别在于它们位于与表单相同的文件夹中。 配置文件不是。 这是我用来渲染表单的内容:

<%= render partial: "statuses/form", locals: { status: @status} %>

这是我的控制器的外观:

class StatusesController < ApplicationController

  before_filter :authenticate_member!, only: [:new, :create, :edit, :update] 

  # GET /statuses
  # GET /statuses.json
  def index
  @statuses = Status.all

    respond_to do |format|
    format.html # index.html.erb
    format.json { render json: @statuses }
   end
  end

  # GET /statuses/1
  # GET /statuses/1.json
  def show
    @status = Status.find(params[:id])

    respond_to do |format|
      format.html # show.html.erb
      format.json { render json: @status }
    end
  end

  # GET /statuses/new
  # GET /statuses/new.json
  def new
    @status = Status.new

    respond_to do |format|
      format.html # new.html.erb
      format.json { render json: @status }
    end
  end

  # GET /statuses/1/edit
  def edit
    @status = Status.find(params[:id])
  end

  # POST /statuses
  # POST /statuses.json
  def create
    @status = current_member.statuses.new(params[:status])

    respond_to do |format|
      if @status.save
        format.html { redirect_to @status, notice: 'Status was successfully created.' }
        format.json { render json: @status, status: :created, location: @status }
      else
        format.html { render action: "new" }
        format.json { render json: @status.errors, status: :unprocessable_entity }
      end
    end
  end

  # PUT /statuses/1
  # PUT /statuses/1.json
  def update
    @status = current_member.statuses.find(params[:id])
    if params[:status] && params[:status].has_key?(:user_id)
        params[:status].delete(:user_id) 
    end 
    respond_to do |format|
      if @status.update_attributes(params[:status])
        format.html { redirect_to @status, notice: 'Status was successfully updated.' }
        format.json { head :no_content }
      else
        format.html { render action: "edit" }
        format.json { render json: @status.errors, status: :unprocessable_entity }
      end
    end
  end

  # DELETE /statuses/1
  # DELETE /statuses/1.json
  def destroy
    @status = Status.find(params[:id])
    @status.destroy

    respond_to do |format|
      format.html { redirect_to statuses_url }
      format.json { head :no_content }
    end
  end
end

我在这里想念什么?

局部变量中的局部变量将是status ,而不是@status

好吧,我终于找到了这个。 我没有尝试渲染它,而是将其直接添加到窗体中以使其如下所示:

<%= simple_form_for(@status) do |f| %>
  <%= f.input :content, label: false, input_html: { class: 'forms', id: 'update_box', tabindex: '1', rows: '1' } %>
  <span class="actions">
   <%= f.submit "Post to Stream", :class => 'reg_button btn btn-info' %>
  </span>
<% end %>

并将@status = Status.new添加到我的Profiles Controller中的show方法中,以便其正确发布:

class ProfilesController < ApplicationController

  def show
    @status = Status.new
    @member = Member.find_by_user_name(params[:id])
    if @member 
        @statuses = @member.statuses.all
        render action: :show
    else
        render file: 'public/404', status: 404, formats: [:html]
    end
  end

end

希望这可以帮助其他人。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM