我使用脚手架构建我的RoR应用程序然后添加了另一个名为apply_configs的控制器操作。它运行正常(我添加到控制器方法的所有操作都执行得很好,我可以从服务器上的日志中告诉它)但是我无法在执行后呈现正确的页面。我在控制器文件
中的apply_configs方法的末尾添加了以下内容respond_to do |format|
if @l2vpn.update_attributes(params[:l2vpn])
format.html { render action: "show", notice: 'L2vpn was successfully updated.' }
format.json { head :ok }
else
format.html { render action: "edit" }
format.json { render json: @l2vpn.errors, status: :unprocessable_entity }
end
end
在服务器上,我可以看到这些日志
Started GET "/l2vpns/apply_configs? <long list of params goes here> " for 172.24.67.151 at 2012-03-16 11:32:01 -0700
Processing by L2vpnsController#apply_configs as */*
Parameters: { <long comma separated list of params goes here> }
L2vpn Load (0.4ms) SELECT "l2vpns".* FROM "l2vpns" WHERE "l2vpns"."id" = ? LIMIT 1 [["id", 1]]
Rendered l2vpns/show.html.erb within layouts/application (1.9ms)
Completed 200 OK in 112ms (Views: 12.4ms | ActiveRecord: 1.2ms)
但事实上,与日志不同,我的浏览器不显示show.html.erb的内容,浏览器页面不变。我的目标是在执行apply_configs后显示show.html.erb。
任何人都知道如何实现这一目标,以及为什么上述配置对我没有预期效果?
jdl评论后的更新代码
class L2vpnsController < ApplicationController
respond_to :js, :html, :json
# GET /l2vpns/1
# GET /l2vpns/1.json
def apply_configs
@l2vpn = L2vpn.find(params[:id])
<more code goes here>
flash[:notice] = "Configs applied successfully!"
respond_with(@l2vpn, :location => l2vpns_url)
end
<other controller action definitions go here>
end
答案 0 :(得分:0)
核心问题是您在更新对象后没有重定向。请查看this Railscast on Rails 3 controllers,特别是与respond_with
有关的部分,这些部分将为您完成大部分工作。
答案 1 :(得分:0)
从我可以收集的内容中,您希望重定向到show
操作,而不是呈现show
模板。通过直接呈现show
模板,您不会收集所需数据(例如模型本身),以便模板正确呈现,因为这是通过Controller.show
方法完成的。
由于我不知道你如何传递受影响模型的id,我认为它是作为名为:id
的参数传递的。
您的重定向看起来就像是假设您在同一个控制器中(否则,通过:controller => 'controller'
传递控制器:
format.html { redirect_to :action => 'show', :id => params[:id], :notice => 'L2vpn was successfully updated.' }
第二个选项[虽然我个人不使用它]实际上是直接渲染show模板,但是你需要复制(或重构和调用)在{中运行的大部分相同代码{1}}方法。更确切地说,您需要运行为视图提供数据的代码部分。
例如:
Controller.show
答案 2 :(得分:0)
我正在猜测,将GET请求更改为POST,这应该可以解决问题。
我猜你的“”超过了GET请求所允许的字符数量,而浏览器会对此产生阻碍。