0

Forgive me if this is a very newby question.

how can i call a method (sharing an object on facebook) when user clicked on the share button in the view.

I can do the share/facebook parts, i just don't know how to call a method from the model when the user clicks on a button

4

2 回答 2

0

根据您的具体需求,我看到了两个可能的答案。

  • 如果要在为对象创建的列之外向模型添加方法,可以在 model.rb 文件中执行此操作:

模型.rb

def name_twice
    "#{self.name}#{self.name}"
end

然后,您可以使用模型的实例来调用它:“@model.name_twice”。

  • 如果你想在控制器中添加另一个路由方法,你可以在你的 models_controller 文件中定义它:

models_controller.rb

def approve
    @model = Model.find_by_id(params[:model_id])
    model.toggle!(:approved)
    redirect_to @model
end

为了使新的控制器功能起作用,您必须将其添加到路由文件中:

路线.rb

resources :models do
   get 'approve', :on => :member 
end

希望这可能会有所帮助。这些示例应该让您了解如何将其他方法/操作添加到模型/控制器。

于 2012-08-16T00:34:17.540 回答
0

my_controller.rb

def do_something
 ...
end

路线.rb

get "/something" => "my_controller#do_something", :as => :do_something
#you can also use post, put, delete or match instead of get

看法

<%= link_to "call do something", do_something_path %>

对于帖子等...

<%= link_to "call do something", do_something_path, method: :post %>
于 2012-08-15T23:24:52.813 回答