使用其他对象的参数创建新对象

发布于 2025-01-08 09:58:53 字数 583 浏览 3 评论 0原文

我的控制器中有一个带有 @post = Post.new 的操作

,在我看来,下一个表单是:

<%= form_for(@post, :method => :get, :remote => true, :url => { :controller => "posts", :action => "new_posts_from_web" }) do |f| %>
<%= f.text_field :remote_posted_url%>
<%= f.submit "Find Images" %>

我想在操作 new_posts_from_web 中创建一个新帖子,其参数由以前的形式,我在 new_posts_from_web 操作中有这个:

def new_posts_from_web
 @post = Post.new(params[:id])
end

但我得到了一个没有参数的新帖子。

如何在其他操作中从表单获取参数而不将对象保存在数据库中?

I have in my controller a action with @post = Post.new

and in my view the next form:

<%= form_for(@post, :method => :get, :remote => true, :url => { :controller => "posts", :action => "new_posts_from_web" }) do |f| %>
<%= f.text_field :remote_posted_url%>
<%= f.submit "Find Images" %>

I want create in action new_posts_from_web a new post with params sent by the previous form, and I have this in new_posts_from_web action:

def new_posts_from_web
 @post = Post.new(params[:id])
end

but I get a new post without params.

How can I get params from a form in other action without save in database the object?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

晨曦÷微暖 2025-01-15 09:58:53
@post = Post.new(params[:post])

应该可以正常工作。

@post = Post.new(params[:post])

should work fine.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文