简体   繁体   中英

How do I create another controller action to create an object in rails?

I have a model called 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.

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.

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":

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.

  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?

  • 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. 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM