adding form_for in Model dialog box rails

95 views Asked by At

I have home controller and email controller. In home controller I have a model dialog box to update the field in email tabel.

So I have created a method in home controller to create new field of email like this

  def esave
  @email = Email.new(user_params)
  if !user_params.nil?
    if @email.save_with_captcha
      flash[:notice] = "Thank you for registering you email address"
      redirect_to :action => 'new'
    else
      render 'new'
    end
  end

end

def user_params
  params.require(:email).permit(:email, :captcha, :captcha_key) if params[:email]
end

Now I want to access this method in my home controller view. and I tried to do so using this

  <%= form_for(@email, url: home_esave_path, :html => {class:"form-signin"}) do |f| %>
< % end % >

but it is throwing error

ActionView::Template::Error (First argument in form cannot contain nil or be empty):
App 31348 stderr:     100:  </div>
App 31348 stderr:     101: 
App 31348 stderr:     102:  <!-- Modal -->
App 31348 stderr:     103:  <%= form_for(@email, url: home_esave_path,    :html => {class:"form-signin"}) do |f| %>
App 31348 stderr:     104:  <% if @email.errors.any? %>
App 31348 stderr:     105:  <div id="error_explanation">
App 31348 stderr:     106:    <h2>
App 31348 stderr:   app/views/home/index.html.erb:103:in ` _app_views_home_index_html_erb__972726411912165480_69960653266320'

I did not add model dialog box code as it is pure html. So any suggestion on how to use email controller object in home controller view.

1

There are 1 answers

0
Ahmad Hussain On

change this:

<%= form_for(@email, url: home_esave_path, :html => {class:"form-signin"}) do |f| %>

with this:

<%= form_for(@email||Email.new, url: home_esave_path, :html => {class:"form-signin"}) do |f| %>