简体   繁体   English

如何创建另一个控制器动作以在Rails中创建对象?

[英]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 虽然可以单独分配“跳过”

  • Define you routes correctly. 正确定义您的路由。 In this case 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.

相关问题 Rails - 如何避免必须从另一个控制器中的create action触发一个控制器中的create操作 - Rails - how do I avoid having to trigger the create action in one controller from the create action in another controller 如何创建Rails控制器动作? - How do I create a rails controller action? Rails - 如何将创建/新操作从一个控制器放入另一个控制器的索引? - Rails - How can I put the create/new action from one controller into the index of another controller? 如何在Rails中使用对象的控制器的create动作用对象的新实例渲染局部对象? - How can I render a partial with a new instance of my object from the create action of that object's controller in Rails? Rails从另一个控制器调用“创建”操作 - Rails Call 'Create' Action from Another Controller 如何使用curl测试我的Rails控制器创建动作..还是可以使用rspec? - how do I use curl to test my Rails controller create action.. or can I use rspec? Rails 4,如何在create.js.erb中执行控制器特定的操作 - Rails 4, how to do a controller specific action in create.js.erb 如何在Rails 3的控制器中为特定操作创建自定义授权规则? - How do I create custom authorization rules to a particular action in a controller in Rails 3? 如何使用rails 4中控制器内生成的变量覆盖create action - how do I override create action with variables generated inside the controller in rails 4 Rails:在将控制器保存到数据库之前,如何在控制器的create动作中使用表单输入变量进行计算? - Rails: How do I make a calculation using form input variables in the create action of a controller before saving it in the db?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM