Ruby on Rails,在笔记应用程序中创建新帖子后重定向



在我的RoR笔记中,记录应用笔记被称为状态。

当用户创建并发布新状态时,我希望他们被重定向到Satuses页面,这是他们所有状态/注释的索引。

现在,它们被重定向到仅显示新状态/注释的页面。

我以为这将是状态控制器"创建"操作中的简单引用,但事实并非如此。一个朋友编写了应用程序的这一部分,那里的JSON和渲染命令让我有点失望。

以下是来自控制器的代码:

# POST /statuses
  # POST /statuses.json
  def create
    @status = current_user.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

从报名表部分:

  <%= f.text_area :content %>
  <div class="row">
    <div class = "small-3 small-centered columns text-center">
      <%= f.submit "Post", class: "radius button text-center" %>
    </div>
  </div>
<% end %>

在这里,您正在使用重定向到显示状态页面的redirect_to @status。

若要重定向到索引页,请使用以下代码

respond_to do |format|
  if @status.save
    format.html { redirect_to statuses_path, 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

最新更新