简体   繁体   English

在Rails 3中修改response_to

[英]Modifying respond_to in Rails 3

Here is the premise: 这是前提:

I have a model called Rules, which has a corresponding controller and view. 我有一个名为Rules的模型,该模型具有相应的控制器和视图。 I'd like the user to be able to create a new rule from their Dashboard, which has a separate view and controller. 我希望用户能够从其仪表板创建一个新规则,该仪表板具有单独的视图和控制器。

The dashboard has three tabs called "Rules", "Rulesets, and "Games." Each tab has its own partial, so for Rules I'm using the _rules.erb partial which looks like this 仪表板具有三个选项卡,分别称为“规则”,“规则集”和“游戏”。每个选项卡都有其自己的部分,因此对于“规则”,我使用的是_rules.erb部分,如下所示

<div id="dashboard-rules" class="dashboard-panel ui-nav-panel">
  <%= button_to'Create A Rule', '#', id: "create-rule-btn", class: "btn btn-primary" %>

  <div id="create-rule-form-container">
  </div>

...

</div>

Using jQuery, I'm loading the contents of the New Rules page which is controlled by the Rules controller, like this... 使用jQuery,我正在加载“新规则”页面的内容,该页面由“规则”控制器控制,像这样...

$('#create-rule-btn').click (e) ->
    e.preventDefault()
    $('#create-rule-form-container').load('/rules/new.html .form-horizontal')

This loads just the form from rules/new.html. 这仅从rules / new.html加载表单。 My problem is that when I click the "Save Rule" button, it redirects to the /rules URL. 我的问题是,当我单击“保存规则”按钮时,它重定向到/ rules URL。 And then either displays errors or says "Rule Saved Successfully." 然后显示错误或说“规则保存成功”。 I'd like for it not to redirect and have the errors show in the dashboard...again, not redirect to /rules URL. 我希望它不要重定向并在仪表板上显示错误...再次,不要重定向到/ rules URL。

This is my Rules create action, which I think needs to be modified, I'm just not sure how: 这是我的“规则创建”操作,我认为需要对其进行修改,但我不确定如何:

def create
  @rule = Rule.new(params[:rule])

  respond_to do |format|
    if @rule.save
      format.html { redirect_to @rule, notice: 'Rule was successfully created.' }
      format.json { render json: @rule, status: :created, location: @rule }
    else
      format.html { redirect_to :controler => "dashboard"}
      format.json { render json: @rule.errors, status: :unprocessable_entity }
    end
  end
end

I know I have to fiddle with how respond_to is working, I just don't know how. 我知道我必须弄弄respond_to是如何工作的,我只是不知道如何。

Remember that you need to set all variable you set in your dashboard in the create action in order for the following to work. 请记住,您需要在create动作中设置在仪表板上设置的所有变量,以使以下各项起作用。 This is because you're rendering the same template as the dashboard so you need to set the variables again. 这是因为您要渲染与仪表板相同的模板,因此需要再次设置变量。

if @rule.save
  format.html { redirect_to dashboard_path, notice: 'Rule was successfully created.' }
  format.json { render json: @rule, status: :created, location: @rule }
else
  # initialize the variables needed on the dashboard here
  format.html { render template: 'dashboard/index' }
  format.json { render json: @rule.errors, status: :unprocessable_entity }
end

One more solution is to submit the form via ajax and just refresh the page when the rule is saved. 另一种解决方案是通过ajax提交表单,并在保存规则后刷新页面。

UPDATE: quick js solution 更新:快速的js解决方案

in the form that creates the rules, add the following 在创建规则的表单中,添加以下内容

form_for @rule, html: { remote: true } do |f|

This will submit the form via ajax. 这将通过ajax提交表单。 In your controller, add a format.js line after each format.json 在您的控制器中,在每个format.json之后添加format.js

format.json { ... }
format.js

then create a app/views/rules/create.js.erb (or haml) file. 然后创建一个app/views/rules/create.js.erb (或haml)文件。 In the file, you can add any js you want so add the following 在文件中,您可以添加任何所需的js,因此请添加以下内容

<% if @rule.errors.any? %>
  # add js here to append the errors in the page
<% else %>
  window.location.reload
<% end %>

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

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