修改Rails 3中的respond

编程入门 行业动态 更新时间:2024-10-28 05:28:20
修改Rails 3中的respond_to(Modifying respond_to in Rails 3)

这是前提:

我有一个名为Rules的模型,它有一个相应的控制器和视图。 我希望用户能够从他们的仪表板创建一个新规则,该仪表板具有单独的视图和控制器。

仪表板有三个选项卡,分别称为“规则”,“规则集”和“游戏”。每个选项卡都有自己的部分,因此对于规则我使用的是_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>

使用jQuery,我正在加载由Rules控制器控制的New Rules页面的内容,就像这样......

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

这只加载来自rules / new.html的表单。 我的问题是,当我单击“保存规则”按钮时,它会重定向到/ rules URL。 然后显示错误或说“规则已成功保存”。 我希望它不会重定向并让错误显示在仪表板中......再次,不会重定向到/ rules URL。

这是我的规则创建操作,我认为需要修改,我只是不确定如何:

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

我知道我必须弄清楚response_to是如何工作的,我只是不知道如何。

Here is the premise:

I have a model called Rules, which has a corresponding controller and view. 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

<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...

$('#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. My problem is that when I click the "Save Rule" button, it redirects to the /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.

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.

最满意答案

请记住,您需要在创建操作中设置您在仪表板中设置的所有变量,以便以下工作。 这是因为您要渲染与仪表板相同的模板,因此您需要再次设置变量。

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

另一种解决方案是通过ajax提交表单,并在保存规则时刷新页面。

更新:快速解决方案

在创建规则的表单中,添加以下内容

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

这将通过ajax提交表单。 在控制器中,在每个format.json之后添加format.js行

format.json { ... } format.js

然后创建一个app/views/rules/create.js.erb (或haml)文件。 在文件中,您可以添加所需的任何js,因此添加以下内容

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

Remember that you need to set all variable you set in your dashboard in the create action in order for the following to work. 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.

UPDATE: quick js solution

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. In your controller, add a format.js line after each format.json

format.json { ... } format.js

then create a app/views/rules/create.js.erb (or haml) file. In the file, you can add any js you want so add the following

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

更多推荐

本文发布于:2023-08-01 10:01:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1356706.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:Rails   respond

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!