在 Rails 中,如何从视图中的表单调用控制器中的特定方法?

发布于 2024-11-25 23:50:14 字数 1051 浏览 1 评论 0原文

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

不疑不惑不回忆 2024-12-02 23:50:14

一个非常重要的提示,您不应该在 Ruby 上使用驼峰命名法作为变量名,变量沙方法应该使用下划线作为分隔符,如果您来自 Java 等其他语言,请尽量避免使用相同的您在那里使用的命名模式。 ruby 中的驼峰式大小写仅适用于类和模块名称。

我猜您正在使用 Ruby on Rails 3,因为您没有说明您正在使用什么。首先,你需要一个路由,它在你的routes.rb文件中看起来像这样:

resources :messages do
    member do 
        post 'close'
    end
end

对你的控制器做一点改变

def close
   @message = Message.find(params[:id]) #you don't need to use the hidden field here
   @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

你的表单将看起来像这样:

<%= form_for( @message, :url => close_message_path( @message ), :html => { :method => :post } ) do |f| %>
    Are you sure you wish to close this message?<br>
    <%= f.submit "Close Message" %>
<% end %>

这个表单将被发布到“/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:

resources :messages do
    member do 
        post 'close'
    end
end

A little change to your controller

def close
   @message = Message.find(params[:id]) #you don't need to use the hidden field here
   @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

And your form is going to look like this:

<%= form_for( @message, :url => close_message_path( @message ), :html => { :method => :post } ) do |f| %>
    Are you sure you wish to close this message?<br>
    <%= f.submit "Close Message" %>
<% end %>

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.

苯莒 2024-12-02 23:50:14

像这样的事情应该为您解决问题:

<%= form_for @message, :url => { :action => "close" }, :html => { :method => :put } do |f| %>

它的作用是指定您正在对消息控制器中的 close 方法执行 HTTP PUT 方法。有关 form_for 辅助方法的更多信息,请阅读 此处

Something like this should square things away for you:

<%= form_for @message, :url => { :action => "close" }, :html => { :method => :put } do |f| %>

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 the form_for helper method read here

黑凤梨 2024-12-02 23:50:14

您的控制器中是否有更新方法,如果没有,您可以这样做:

<%= form_for( @message ) do |f|
  <p>Are you sure you wish to close this message?</p>
  <%= hidden_field_tag :close %>
  <%= f.submit "Close Message" %>
<% end %>

然后在您的控制器中:

class MessagesController < ApplicationController

  def update
    @message = Message.find(params[:id])
    @message.completeDate = Date.today if params.has_key?(:close)
    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

Do you have an update method in your controller, if not you can just do this:

<%= form_for( @message ) do |f|
  <p>Are you sure you wish to close this message?</p>
  <%= hidden_field_tag :close %>
  <%= f.submit "Close Message" %>
<% end %>

Then in your controller:

class MessagesController < ApplicationController

  def update
    @message = Message.find(params[:id])
    @message.completeDate = Date.today if params.has_key?(:close)
    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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文