Home > Blockchain >  Rails new.html.erb is not recieving values or not passsing values to controller. from to get new val
Rails new.html.erb is not recieving values or not passsing values to controller. from to get new val

Time:12-29

Rails new.html.erb is not recieving values or not passing values to the controller. from to get new values.

following is my new.html.erb

def new
  @departmentdetail=DepartmentDetail.new
end

def create
  @departmentdetail=DepartmentDetail.new(department_param)
  if @departmentdetail.save
    redirect_to ('index')
  else
    redirect_to('new')
  end
end

def department_param
  params.require(:departmentdetail).permit(:departmentname, :departmentemail, :password)
end
<%= form_for(@departmentdetail) do |f| %> 
  <table summary="Subject for fields"> 
    <tr> 
      <th> <%= f.label :departmentname %><br></th> 
      <td><%= f.text_field :departmentname %></td> 
    </tr> 
    <tr> 
      <th> <%= f.label :departmentemail %><br></th> 
      <td><%= f.text_field :departmentemail %></td> 
    </tr> 
    <tr> 
      <th> <%= f.label :password %><br></th> 
      <td><%= f.text_field :password %></td> 
    </tr>
  </table> 
  <%= f.submit("Create Department")%>

CodePudding user response:

You are not fully following Ruby on Rails naming conventions. When there is a class named DepartmentDetail then the variables and the keys in the strong params should be named with an underscore like this department_detail. Additionally, redirecting to new instead of just rendering new in the case of an error when saving will result in losing the current state:

I suggest changing your controller to:

def new
  @department_detail = DepartmentDetail.new
end

def create
  @department_detail = DepartmentDetail.new(department_detail_params)
  
  if @department_detail.save
    redirect_to :index
  else
    render :new
  end
end

def department_detail_params
  params.require(:department_detail).permit(:departmentname, :departmentemail, :password)
end

And your view to:

<%= form_for(@department_detail) do |f| %> 
  <table summary="Subject for fields"> 
    <tr> 
      <th> <%= f.label :departmentname %><br></th> 
      <td><%= f.text_field :departmentname %></td> 
    </tr> 
    <tr> 
      <th> <%= f.label :departmentemail %><br></th> 
      <td><%= f.text_field :departmentemail %></td> 
    </tr> 
    <tr> 
      <th> <%= f.label :password %><br></th> 
      <td><%= f.text_field :password %></td> 
    </tr>
  </table> 
  <%= f.submit("Create Department")%>
<% end %>

CodePudding user response:

When the user submits an invalid form you want to render - not redirect.

def create
  @departmentdetail = DepartmentDetail.new(department_param)
  if @departmentdetail.save
    redirect_to action: :index
  else
    render :new
  end
end

This will render the new.html.erb view so that the user gets the form back but with messages about the validation errors.

On a technical level what you're actually doing here is rendering a response to the user sending a non-idempotent request - its a response custom tailored to that request. This is very different from the new action which is idempotent (the form looks the same for everyone).

If you redirect the user you'll lose all the user input and the validation messages.

  • Related