【问题标题】:Rails - Rendering a view with a different action?Rails - 用不同的动作渲染视图?
【发布时间】:2017-03-11 04:32:14
【问题描述】:

据我了解,您可以使用渲染在视图中创建视图。我一直试图使用它是有一个显示页面,在这个页面中,有一些按钮。一个按钮将显示/隐藏用于创建活动的表单。本质上,该页面将显示活动并允许用户通过单击相应的按钮并填写表单来创建更多活动。

我试图呈现与“新”操作相对应的“新”页面,但遇到了以下问题

活动中的 NoMethodError#display

未定义的方法`to_key' 活动::ActiveRecord_Relation:0x694e270

搜索此错误,我认为它涉及我的活动控制器中的“显示”操作以及它如何不期望“form_for”。我不知道如何解决这个问题,或者我是否正确使用了渲染。

我想知道这是可能的还是推荐的,因为我看到的示例似乎在 html 之间存在共享元素时使用渲染,因此使用渲染会减少代码。显示页面将是唯一具有用于创建活动的表单的页面,因此我只需添加 html 代码即可。

任何建议或帮助将不胜感激,谢谢!

活动显示 HTML:

<nav aria-label="...">

  <% @activity.each do |activity| %>

      <div class = "row text-center">
        <div class = 'col-md-4'>
          <h3>
            <%= activity.a_name %>
          </h3>
        </div>
      </div>

  <% end %>

  <div class="panel-group">
    <div class="panel-body">
      <h2 align = "center">Activities</h2>
    </div>
    <div class="panel-group">
      <div class="panel text-center">
        <div class="panel-heading">
          <h4 class="panel-title">
            <a data-toggle="collapse" href="#collapse1">Collapsible list group</a>
          </h4>
        </div>
        <div id="collapse1" class="panel-collapse collapse">
          <ul class="list-group">
            <li class="list-group-item">
              <h1>ONE</h1>
            </li>
            <li class="list-group-item">Two</li>
            <li class="list-group-item">Three</li>
          </ul>
          <div class="panel-footer">Footer</div>
        </div>
      </div>

    </div>
  </div>


  <ul class="pager">

    <li class="previous"><a href="#"><span aria-hidden="true">&larr;</span>Previous </a></li>
    <li class="next"><a href="#"> Next<span aria-hidden="true">&rarr;</span></a></li>
    <div class="btn-group" role="group" aria-label="...">
      <a href="new" class="btn_act">Add Activity</a>
    </div>

    <% render 'activities/new'%>

  </ul>


</nav>

活动_新 HTML:

<body>
<% flash.each do |key, value| %>
    <div class="alert alert-<%= key %>"><%= value %></div>
<% end %>

<%= form_for @activity do |a| %>
    <div class = "form-group">
      <div class = "row top-buffer text-center">
        <div class='col-md-3'>
          <%= a.label :a_name, 'Activity Name' %>:
          <%= a.text_field :a_name %>
        </div>
      </div>

      <div class ="row top-buffer text-center">
        <div class="col-md-3">
          <%= a.submit 'Create', class: 'btn btn-primary'%>
        </div>
      </div>
    </div>
<% end %>

</body>

活动控制器:

class ActivitiesController < ApplicationController
  def display
    @activity = Activity.all

  end

  def index
    @activity = Activity.all
  end

  def show
    @activity = Activity.all
  end

  def new
    @activity = Activity.new
  end

  def create
    @activity = Activity.create(activity_params)
    if @activity.save!
      flash[:success] = 'Activity created successfully!'
      redirect_to activities_display_path
    else
      flash[:error] = 'ERROR: Activity was not saved!'
      #render_to_string #normally would have it render to the name of view ex: :new
    end
  end

  def edit
    @activity = Activity.find(params[:id])
  end

  def update
    @activity = Activity.update(activity_params)
    if @activity.save
      flash[:success] = 'Activity successfully updated!'
      redirect_to root
    else
      flash[:error] = 'ERROR: Activity failed to update'
      render_to_string
    end
  end

  private
    def activity_params
      params.require(:activity).permit(:a_name)
    end

end

编辑 - 2017 年 3 月 10 日 设法修复了上面的错误。原来我需要添加 “@activity = Activity.new”进入控制器中的“显示”动作。之后,我将“@activity = Activity.all”更改为“@activities = Activity.all”。 然后在显示视图中,我改变了

  <% @activity.each do |activity| %>

  <% @activities.each do |activity| %>

渲染成功。感谢所有的建议和帮助!

【问题讨论】:

    标签: html ruby-on-rails


    【解决方案1】:

    作为命名约定,我会将@activities 重命名为@activity 在您的新、创建、编辑和更新操作及其关联视图中。至于错误,form_for 应该用于单个对象,而不是集合。应该是这样的:

      <% @activities.each do |activity| %>
          <div class = "row text-center">
            <div class = 'col-md-4'>
              <h3>
                <%= activity.a_name %>
              </h3>
            </div>
          </div>
      <% end %>
    

    【讨论】:

    • 好的,谢谢您的建议!我相应地更新了控制器和视图。但是我仍然有相同的错误(NoMethodError),这是由于使用 form_for 的“_new”视图中的 form_for。我不知道如何解决这个问题,因为我认为我需要 form_for 才能使用用户输入创建模型实例。
    • 设法解决了这个问题。谢谢你的建议
    猜你喜欢
    • 1970-01-01
    • 2013-12-06
    • 2013-04-02
    • 2011-09-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多