在 Rails 中,如何从视图中的表单调用控制器中的特定方法?
Rails 新手,请耐心等待。我想做的是屏幕上有一个消息列表。我希望将消息标记为“已关闭”,这意味着它只填写完整日期。
我有一个模型:
class Message < ActiveRecord::Base
attr_accessible :topic, :body, :completeDate, :created_at
belongs_to :account
belongs_to :user
has_many :message_followups
end
我有一个控制器:
class MessagesController < ApplicationController
def close
@message = Message.find(params[:messageId])
@message.completeDate = Date.today
if @message.save
redirect_to myHomeMessages_path, :flash => { :success => "Your message was closed." }
else
redirect_to myHomeMessages_path, :flash => { :error => "Error closing message." }
end
end
end
我有一个视图:
<%= form_for (???) do |f| %>
Are you sure you wish to close this message?<br>
<%= hidden_field_tag 'messageId', message.id.to_s %>
<%= submit_tag "Close Message" %>
<% end %>
我在弄清楚如何获取 form_for 或 form_tag 来调用消息控制器中的特定方法“close”时遇到问题。任何帮助将不胜感激,谢谢。
马特
new to rails so bear with me. What I would like to do, is I have a list of messages on the screen. I want the message to be marked as 'closed' which means it just fills in a completeDate.
I have a model:
class Message < ActiveRecord::Base
attr_accessible :topic, :body, :completeDate, :created_at
belongs_to :account
belongs_to :user
has_many :message_followups
end
I have a controller:
class MessagesController < ApplicationController
def close
@message = Message.find(params[:messageId])
@message.completeDate = Date.today
if @message.save
redirect_to myHomeMessages_path, :flash => { :success => "Your message was closed." }
else
redirect_to myHomeMessages_path, :flash => { :error => "Error closing message." }
end
end
end
I have a view:
<%= form_for (???) do |f| %>
Are you sure you wish to close this message?<br>
<%= hidden_field_tag 'messageId', message.id.to_s %>
<%= submit_tag "Close Message" %>
<% end %>
I'm having a problem figuring out how to get the form_for or a form_tag to call the specific method 'close' in the messages controller. Any help would be greatly appreciated, thanks.
Matt
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
一个非常重要的提示,您不应该在 Ruby 上使用驼峰命名法作为变量名,变量沙方法应该使用下划线作为分隔符,如果您来自 Java 等其他语言,请尽量避免使用相同的您在那里使用的命名模式。 ruby 中的驼峰式大小写仅适用于类和模块名称。
我猜您正在使用 Ruby on Rails 3,因为您没有说明您正在使用什么。首先,你需要一个路由,它在你的routes.rb文件中看起来像这样:
对你的控制器做一点改变
你的表单将看起来像这样:
这个表单将被发布到“/messages/ID_HERE” /close”,Rails 将根据您的请求将“ID_HERE”值设置为“id”参数。
您可以在此处查看有关 form_for 的完整文档。
A very important hint, you should not use camel case for variable names on Ruby, variable sand methods should use underscore as separators, if you're coming from another language like Java, try to avoid using the same naming patterns you used in there. Camel case in ruby is only for class and module names.
I am guessing you're using Ruby on Rails 3, as you haven't said what you're using. First, you need a route for that, it would look like this on your routes.rb file:
A little change to your controller
And your form is going to look like this:
This form is going to be posted to "/messages/ID_HERE/close" and Rails is going to set the "ID_HERE" value as the "id" parameter on your request.
You can see the full documentation about form_for here.
像这样的事情应该为您解决问题:
它的作用是指定您正在对消息控制器中的
close
方法执行 HTTP PUT 方法。有关form_for
辅助方法的更多信息,请阅读 此处Something like this should square things away for you:
What this does is specify that you are performing an HTTP PUT method to the
close
method in the messages controller. For more information about theform_for
helper method read here您的控制器中是否有更新方法,如果没有,您可以这样做:
然后在您的控制器中:
Do you have an update method in your controller, if not you can just do this:
Then in your controller: