[英]How do I create another controller action to create an object in rails?
I have a model called Contact_Email. 我有一个名为Contact_Email的模型。 When an Email template is sent through ActionMailer to a specific Contact, as part of the Create action it sends it through upon .save.
当电子邮件模板通过ActionMailer发送到特定联系人时,作为“创建”操作的一部分,它将通过.save发送出去。
However, I want to create a "skip" action which also creates a Contact_Email, but does NOT send an ActionMailer and allows me to set the status differently. 但是,我想创建一个“跳过”操作,该操作也创建一个Contact_Email,但是不发送ActionMailer,并且允许我以不同的方式设置状态。
I want to create a separate action because I want to make this respond to a remote_for_tag so that I can just have an ajax button indicate it has been "skipped": 我想创建一个单独的动作,因为我想让它响应remote_for_tag,这样我就可以有一个ajax按钮来表明它已被“跳过”:
Here's what I tried, but while it creates a Contact_Email, I end up getting an error when I want to go back and view all the Contacts again. 这是我尝试过的方法,但是当它创建一个Contact_Email时,当我想返回并再次查看所有联系人时,却遇到了错误。
def skip
@contact_email = ContactEmail.new
@contact_email.contact_id = params[:contact_id]
@contact_email.email_id = params[:email_id]
@contact_email.status = "skipped"
if @contact_email.save
flash[:notice] = "skipped email"
redirect_to contact_emails_url
end
end
Well, your code seems ok, just a few things. 好吧,您的代码似乎还可以,仅需注意几件事。
You have no else statement, what if your @contact_email is not saved? 您没有其他声明,如果您的@contact_email没有保存怎么办?
You should definately NOT assign all of the params separately. 您绝对不应单独分配所有参数。 Use a form in the view for contact_email and
@contact_email = ContactEmail.new(params[:contact_email])
in controller. 在视图中为控制器中的contact_email和
@contact_email = ContactEmail.new(params[:contact_email])
使用表单。 Though having "skipped" assigned separately is ok 虽然可以单独分配“跳过”
map.resources :contact_emails, :member => {:skip => :post}
And everything should be just fine map.resources :contact_emails, :member => {:skip => :post}
并且一切都应该很好
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.