Rob
Rob

Reputation: 1855

form_for is creating a new record but the values are blank

When I submit this form a new record is made but the values aren't being added. Tried a few different things but nothing is working

comments controller

 def report

    @report_comment = ReportComment.new
    if @report_comment.save(report_comment_params)

      redirect_to root_path
    else
      render 'static_pages/home'
    end
  end

 private
  def report_comment_params
    params.require(:report_comment).permit(:guide_id, :submitted_by, :comment_id)

end

comments view (just the report form)

<% @report_comment = ReportComment.new %>
<% @guide = Guide.friendly.find(params[:guide_id]) %>
<% if current_user.nil?  %>
<% user_id = 0 %>
<% else %>
<% user_id = current_user.id %>
<% end %>
          <%= form_for([@report_comment], url: comments_report_path, method: :post) do |f| %>
      <%= f.hidden_field :guide_id, value: @guide.id %>
      <%= f.hidden_field :submitted_by, value: user_id %>
      <%= f.hidden_field :comment_id, value: comment.id %>

        <%= f.submit "Report" %>

    <% end %>

log with the params passed

 Parameters: {"utf8"=>"✓", "authenticity_token"=>"0XMjwzMlMLSwqpImXpjTRWd8yHUP/ERTkvnzd9rWIkSA94l9f9bCQdODXrC3SyvmfJjuW/N3zkp5pVZAf+0D+w==", "report_comment"=>{"guide_id"=>"1", "user_id"=>"1", "comment_id"=>"1"}, "commit"=>"Report"}

Not sure why its not saving the values. I can alternatively add @report_comment.update(guide_id: params[:report_comment][:guide_id], submitted_by: params[:report_comment][:submitted_by], comment_id: params[:report_comment][:comment_id]) under if @report_comment.save(report_comment_params) but that isn't ideal and not as good as just having them save straight from the form submit.

Upvotes: 0

Views: 76

Answers (4)

Sukanta
Sukanta

Reputation: 585

Pass the params value to .new Instantiate method. You can't pass params to save method

def report
  @report_comment = ReportComment.new(report_comment_params)
  if @report_comment.save
    redirect_to root_path
  else
    render 'static_pages/home'
  end
end

or you can user create method instead of save

def report
  @report_comment = ReportComment.create(report_comment_params)
  if @report_comment.present?
    redirect_to root_path
  else
    render 'static_pages/home'
  end
end

Upvotes: 1

HashRocket
HashRocket

Reputation: 798

You controller is wrong, you should pass the parameter in ReportComment.new not in .save:

def report

  @report_comment = ReportComment.new(report_comment_params)
  if @report_comment.save
    redirect_to root_path
  else
    render 'static_pages/home'
  end
end

And much better if you add validations in your model.

Upvotes: 0

LHH
LHH

Reputation: 3323

Assign params to new method and then save it

def report
  @report_comment = ReportComment.new(report_comment_params)
  if @report_comment.save
    redirect_to root_path
   else
     render 'static_pages/home'
   end
 end

Upvotes: 1

Shishir
Shishir

Reputation: 780

Pass params to new

@report_comment = ReportComment.new(report_comment_params)
if @report_comment.save

  redirect_to root_path
else
  render 'static_pages/home'
end

Upvotes: 0

Related Questions