你说dosomething的是一个动作。这意味着,它在单独的 HTTP 请求中被调用。
有几种方法可以在请求之间共享数据:
- 存储在会话中
- 将其存储在表单的隐藏字段中,如果
dosomething是表单的操作
- 将其作为参数转发,如果
dosomething由 a 调用link_to
- if
dosomething是 a 的一个动作post,所有这些都在 the 中,PostsController并且您有一条通往该动作的路线,那么:
在您的展示视图中使用
<%= link_to 'do something', dosomething_post_path(@post) %>
在你的行动中
def dosomething
@currentpost = Post.find(params[:id])
....
end
在你的routes.rb你需要类似的东西
resources :posts do
member do
get 'dosomething'
end
end
或使用表格:
在您看来:
<%= form_for @message, :url => {:action => "dosomething"}, :method => "post" do |f| %>
<%= hidden_field_tag :post_id, @post.id %>
...
在您的控制器中:
def dosomething
@currentpost = Post.find(params[:post_id])
....
end