【问题标题】:In Rails, how do you call a specific method in a controller from a form in a view?在 Rails 中,如何从视图中的表单调用控制器中的特定方法?
【发布时间】:2011-07-24 16:30:58
【问题描述】:

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 在消息控制器中调用特定方法“关闭”时遇到问题。任何帮助将不胜感激,谢谢。

马特

【问题讨论】:

标签: ruby-on-rails view model controller


【解决方案1】:

一个非常重要的提示,你不应该在 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”参数。

You can see the full documentation about form_for here.

【讨论】:

  • 谢谢 Mauricio,你说得对,我使用的是 Rails 3,正如你所说,我来自 Java 背景。感谢您的提示,我将来会更加注意我的命名约定。也感谢您的详细回复,这更有意义。
  • 不客气,当我从 Java 迁移到 Ruby 时,我也对变量名有过这种时髦的想法,但今天我可以看到这两种情况的美 :)
【解决方案2】:

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

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

这样做是指定您正在对消息控制器中的close 方法执行HTTP PUT 方法。有关form_for 辅助方法的更多信息,请阅读here

【讨论】:

    【解决方案3】:

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

    <%= 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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-09-26
      • 2011-02-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多