1

I'm trying to have a simple button to update for each Comment through a submit form. Here is my View code:

<% @comments.each do |comment| %>
    <%= form_for comment, url: article_comment_path(comment.article, comment), method: :patch do |f| %>
        <%= hidden_field_tag :update_time, Time.now %>
        <%= f.submit "Confirm" %>
    <% end %>
<% end %>

Comments Controller Update Action Code:

def update
  @article = Article.friendly.find(params[:article_id])
  @comment = @user.comments.find(params[:id])

  if @comment.update(comment_params)
    redirect_to @comments
  else
    render article_comments_path(@article)
  end
end

private
        def comment_params
          params.require(:comment).permit(:date, :note)
        end

With the code above, I'm getting this error:

param is missing or the value is empty: comment - the error highlights the params.require line in the private declaration

svelandiag
  • 4,231
  • 1
  • 36
  • 72
gitastic
  • 516
  • 7
  • 26

2 Answers2

0

Your issue here is pretty simple, look at your form, you do not have any :note so when you try to require :note in your params hash then you get that error because there is not :note key in your params hash, for get around this you have two options:

  1. Create another params method and use it conditionally:

    private def comment_params params.require(:comment).permit(:date, :note) end def comment_params_minimal params.require(:comment).permit(:date) end

and then use it conditionally in your update action:

def update
  @article = Article.friendly.find(params[:article_id])
  @comment = @user.comments.find(params[:id])
  if params[:comment][:note].present?
    use_this_params = comment_params
  else
    use_this_params = comment_params_minimal
  end
  if @comment.update(use_this_params)
    redirect_to @comments
  else
    render article_comments_path(@article)
  end
end
  1. The other way is tu directly update your comment using params hash instead of whitelist them with comment_params so if params[:comment][:note].present? update in the normal fashion else, update only the date attribute directly: params[:comment][:date]

Hope this helps you.

svelandiag
  • 4,231
  • 1
  • 36
  • 72
-1

You are submitting to the article comment path, but your form is for article(like in your code <%= form_for article) and not comment. Thus the params you should look for first is the article params[:article]. I think if you put a debugger like this

def update
  debugger #<<<<<<<<<
  @article = Article.friendly.find(params[:article_id])
  @comment = @user.comments.find(params[:id])

  if @comment.update(comment_params)
    redirect_to @comments
  else
    render article_comments_path(@article)
  end
end

You can then check the params that is submitting to your controller update action. And most likely you will find your comment params in your article params like

params[:article][:comment]

but I am just guessing here. With debugger and server log you can check exactly what param were submitted to your update action.

uoroaix
  • 97
  • 6