Save 36% for Black Friday! Learn more

All threads / How to fix params require for form_for contact in multi controller view

Ask A Question


You’re not receiving notifications from this thread.

How to fix params require for form_for contact in multi controller view

Anthony BESSON asked in Rails

I have a problem in my contact form_for, I have one contact form and I want to render my contact form in my product view. If I make that I have a problem with my require params

My contact_params work in my ContactController:

def contact_params
   params.require(:contact).permit(:name, :email, :body)

I have add in my ApplicationController

def set_contact
   @contact = params[:contact] || {}

and that in my ProductsControler

   @contact = :set_contact

my route are:

get 'contact', to: 'contacts#new', as: 'new_contact'
post 'contact', to: 'contacts#create', as: 'create_contact'

my render is same that:

<%= render :partial => "contacts/contact" %>

and my view in _contact.html.erb:

 <%= form_for @contact, url: create_contact_url do |f| %>
  <%= f.text_field :name  %>
  <%= f.email_field :email %>
  <%= f.text_area :body  %>
 <%= f.submit 'Send' %>

Rails say me:

ActionController::ParameterMissing (param is missing or the value is empty: contact):

If I delete require(:contact) that not work rails say me:

Unpermitted parameter: :contact


in ProductsControler


   @contact = :set_contact


   @contact = set_contact

juste delete :

Join the discussion

Want to stay up-to-date with Ruby on Rails?

Join 54,329+ developers who get early access to new tutorials, screencasts, articles, and more.

    We care about the protection of your data. Read our Privacy Policy.

    logo Created with Sketch.

    Ruby on Rails tutorials, guides, and screencasts for web developers learning Ruby, Rails, Javascript, Turbolinks, Stimulus.js, Vue.js, and more. Icons by Icons8

    © 2021 GoRails, LLC. All rights reserved.