Rails Form Data Not Getting Saved to Db

Form_for not saving data to database

Just do like:

  def create
@reg = Register.new(params.required(:register).permit(:user_id, :class_name))
@reg.user_id = current_user.id
@reg.save! # This will raise exception if something is wrong
end

Btw, just apply to TDD/BDD concepts to eliminate problems like this one. This process is pretty simple.

rails form data not getting saved to db

if you are using form_for, use the form_for syntax

<%= form_for(@calculate) do |form| %>
<%= form.label :number %>
<%= form.text_field :number %>
<%= form.label :root %>
<%= form.text_field :root %>
<%= form.submit "Submit" %>
<% end %>

this will automatically handle the routes if the @calculate is new object it will submit it to create or if it is already saved it will send a put request to edit action

Form fields are not saved into database - ruby on rails

new is the wrong method in which to perform #save. that should be done in create

  def new
@landslide = Landslide.new
end

def create
@landslide = Landslide.new(landslide_params)
if @landslide.save
render plain: @landslide.inspect
else
render :new
end
end

Also, your form looks wrong. The location of the form data in your returned params depends on the control's name field, not on the id

Instead of

%input#location.form-control{:type => "Location"}

I'd expect to see

%input#landslide_location.form-control{:type => "text", :name => "landslide[location]"}

Form fields not saving to DB: does this have to do with how form parameters are passed for new/create controller methods?

What is the correct way to pass in parameters for the methods
‘create' and ‘new’?

Your form looks good for me.

is there anything else in the code given here that might be preventing
posts from being saved to DB?

Your create action doesn't save a record.

 def create
@post = Post.new(post_params)
@post.save
redirect_to '/'
end

The errors I see are something about missing.png — I’m assuming this
is because there is no default image loaded, one called “missing.png”.

Right



Related Topics



Leave a reply



Submit