Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Redirect to @record or path in controller generator #569

Merged
merged 1 commit into from
Sep 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions lib/generators/rails/templates/controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ def create

respond_to do |format|
if @<%= orm_instance.save %>
format.html { redirect_to <%= show_helper %>, notice: <%= %("#{human_name} was successfully created.") %> }
format.html { redirect_to <%= redirect_resource_name %>, notice: <%= %("#{human_name} was successfully created.") %> }
format.json { render :show, status: :created, location: <%= "@#{singular_table_name}" %> }
else
format.html { render :new, status: :unprocessable_entity }
Expand All @@ -43,7 +43,7 @@ def create
def update
respond_to do |format|
if @<%= orm_instance.update("#{singular_table_name}_params") %>
format.html { redirect_to <%= show_helper %>, notice: <%= %("#{human_name} was successfully updated.") %> }
format.html { redirect_to <%= redirect_resource_name %>, notice: <%= %("#{human_name} was successfully updated.") %> }
format.json { render :show, status: :ok, location: <%= "@#{singular_table_name}" %> }
else
format.html { render :edit, status: :unprocessable_entity }
Expand All @@ -57,7 +57,7 @@ def destroy
@<%= orm_instance.destroy %>

respond_to do |format|
format.html { redirect_to <%= index_helper %>_url, notice: <%= %("#{human_name} was successfully destroyed.") %> }
format.html { redirect_to <%= index_helper %>_path, notice: <%= %("#{human_name} was successfully destroyed.") %> }
format.json { head :no_content }
end
end
Expand Down
12 changes: 6 additions & 6 deletions test/scaffold_controller_generator_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,22 +31,22 @@ class ScaffoldControllerGeneratorTest < Rails::Generators::TestCase
assert_instance_method :create, content do |m|
assert_match %r{@post = Post\.new\(post_params\)}, m
assert_match %r{@post\.save}, m
assert_match %r{format\.html \{ redirect_to post_url\(@post\), notice: "Post was successfully created\." \}}, m
assert_match %r{format\.html \{ redirect_to @post, notice: "Post was successfully created\." \}}, m
assert_match %r{format\.json \{ render :show, status: :created, location: @post \}}, m
assert_match %r{format\.html \{ render :new, status: :unprocessable_entity \}}, m
assert_match %r{format\.json \{ render json: @post\.errors, status: :unprocessable_entity \}}, m
end

assert_instance_method :update, content do |m|
assert_match %r{format\.html \{ redirect_to post_url\(@post\), notice: "Post was successfully updated\." \}}, m
assert_match %r{format\.html \{ redirect_to @post, notice: "Post was successfully updated\." \}}, m
assert_match %r{format\.json \{ render :show, status: :ok, location: @post \}}, m
assert_match %r{format\.html \{ render :edit, status: :unprocessable_entity \}}, m
assert_match %r{format\.json \{ render json: @post.errors, status: :unprocessable_entity \}}, m
end

assert_instance_method :destroy, content do |m|
assert_match %r{@post\.destroy}, m
assert_match %r{format\.html \{ redirect_to posts_url, notice: "Post was successfully destroyed\." \}}, m
assert_match %r{format\.html \{ redirect_to posts_path, notice: "Post was successfully destroyed\." \}}, m
assert_match %r{format\.json \{ head :no_content \}}, m
end

Expand All @@ -64,15 +64,15 @@ class ScaffoldControllerGeneratorTest < Rails::Generators::TestCase
run_generator %w(Admin::Post --model-name=Post)
assert_file 'app/controllers/admin/posts_controller.rb' do |content|
assert_instance_method :create, content do |m|
assert_match %r{format\.html \{ redirect_to admin_post_url\(@post\), notice: "Post was successfully created\." \}}, m
assert_match %r{format\.html \{ redirect_to \[:admin, @post\], notice: "Post was successfully created\." \}}, m
end

assert_instance_method :update, content do |m|
assert_match %r{format\.html \{ redirect_to admin_post_url\(@post\), notice: "Post was successfully updated\." \}}, m
assert_match %r{format\.html \{ redirect_to \[:admin, @post\], notice: "Post was successfully updated\." \}}, m
end

assert_instance_method :destroy, content do |m|
assert_match %r{format\.html \{ redirect_to admin_posts_url, notice: "Post was successfully destroyed\." \}}, m
assert_match %r{format\.html \{ redirect_to admin_posts_path, notice: "Post was successfully destroyed\." \}}, m
end
end
end
Expand Down
Loading