Skip to content

Commit

Permalink
Merge pull request #2638 from MushroomObserver/fix-article-form-model…
Browse files Browse the repository at this point in the history
…-missing

Guarantee @object defined in two forms
  • Loading branch information
nimmolo authored Jan 5, 2025
2 parents 7222528 + 83658cc commit c1eb953
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 9 deletions.
14 changes: 7 additions & 7 deletions app/controllers/articles_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -64,13 +64,13 @@ def edit
# ---------- Actions to Modify data: (create, update, destroy, etc.) ---------

def create
return render(:new) if flash_missing_title?

@article = Article.new(
title: params[:article][:title],
body: params[:article][:body],
title: params.dig(:article, :title),
body: params.dig(:article, :body),
user_id: @user.id
)
return render(:new) if flash_missing_title?

@article.save
redirect_to(article_path(@article.id))
end
Expand All @@ -79,8 +79,8 @@ def update
@article = Article.find(params[:id])
return render(:edit) if flash_missing_title?

@article.title = params[:article][:title]
@article.body = params[:article][:body]
@article.title = params.dig(:article, :title)
@article.body = params.dig(:article, :body)

save_any_changes
redirect_to(article_path(@article.id))
Expand All @@ -105,7 +105,7 @@ def ignore_request_unless_permitted

# add flash message if title missing
def flash_missing_title?
return false if params[:article][:title].present?
return false if params.dig(:article, :title).present?

flash_error(:article_title_required.t)
true
Expand Down
6 changes: 4 additions & 2 deletions app/views/controllers/names/trackers/_form.html.erb
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
<%
# This form can create OR update a tracker, via params[:commit]
# Idk why rails does not recognize the model scope and nest the attributes.
form_args = { url: action, method: method, id: "name_tracker_form" }
form_args[:model] = @name_tracker if @name_tracker
%>
<%= form_with(model: @name_tracker, url: action, method: method,
id: "name_tracker_form") do |f| %>

<%= form_with(**form_args) do |f| %>

<div class="text-center my-3">
<%= if @name_tracker
Expand Down

0 comments on commit c1eb953

Please sign in to comment.