From 4643fbd92ec447cbd2fa981120b353ed22f2cdc7 Mon Sep 17 00:00:00 2001 From: "google-labs-jules[bot]" <161369871+google-labs-jules[bot]@users.noreply.github.com> Date: Fri, 1 May 2026 11:35:58 +0000 Subject: [PATCH] Associate post with crop from crop show page Co-authored-by: CloCkWeRX <365751+CloCkWeRX@users.noreply.github.com> --- app/controllers/posts_controller.rb | 4 ++++ app/views/crops/_actions.html.haml | 3 +++ app/views/crops/_posts.html.haml | 2 +- spec/controllers/posts_controller_spec.rb | 14 ++++++++++++++ 4 files changed, 22 insertions(+), 1 deletion(-) diff --git a/app/controllers/posts_controller.rb b/app/controllers/posts_controller.rb index 4b24953fb..90a400bb1 100644 --- a/app/controllers/posts_controller.rb +++ b/app/controllers/posts_controller.rb @@ -21,6 +21,10 @@ class PostsController < ApplicationController def new @post = Post.new @forum = Forum.find_by(id: params[:forum_id]) + if params[:crop_id] + @crop = Crop.friendly.find(params[:crop_id]) + @post.body = "[#{@crop.name}](crop)" + end respond_with(@post) end diff --git a/app/views/crops/_actions.html.haml b/app/views/crops/_actions.html.haml index 9f0d2d3a2..8aa1f7136 100644 --- a/app/views/crops/_actions.html.haml +++ b/app/views/crops/_actions.html.haml @@ -5,5 +5,8 @@ = render 'plantings/modal', planting: Planting.new(crop: crop, owner: current_member) = render 'harvests/modal', harvest: Harvest.new(crop: @crop, owner: current_member) = render 'seeds/modal', seed: Seed.new(crop: @crop, owner: current_member) + = link_to new_post_path(crop_id: crop.slug), class: 'btn', id: 'post-button' do + = post_icon + Post - if active_plantings.any? = render 'plantings/failed_modal', crop: crop, active_plantings: active_plantings diff --git a/app/views/crops/_posts.html.haml b/app/views/crops/_posts.html.haml index ad0c615ed..975087c66 100644 --- a/app/views/crops/_posts.html.haml +++ b/app/views/crops/_posts.html.haml @@ -6,7 +6,7 @@ Nobody has posted about #{crop.name.pluralize} yet. %p - if can? :create, Post - = link_to "Post something", new_post_path, class: 'btn btn-default' + = link_to "Post something", new_post_path(crop_id: crop.slug), class: 'btn btn-default' - else = render partial: "shared/signin_signup", locals: { to: "post your tips and experiences growing #{crop.name.pluralize}" } diff --git a/spec/controllers/posts_controller_spec.rb b/spec/controllers/posts_controller_spec.rb index 2e0cf3bf3..0462a930a 100644 --- a/spec/controllers/posts_controller_spec.rb +++ b/spec/controllers/posts_controller_spec.rb @@ -10,6 +10,20 @@ describe PostsController do { author_id: member.id, subject: "blah", body: "blah blah" } end + describe '#new' do + let(:crop) { create(:crop, name: 'Bush Bean') } + + it 'pre-populates the body when crop_id is present' do + get :new, params: { crop_id: crop.slug } + expect(assigns(:post).body).to eq("[#{crop.name}](crop)") + end + + it 'does not pre-populate the body when crop_id is absent' do + get :new + expect(assigns(:post).body).to be_nil + end + end + describe '#index' do before do create_list(:post, 100)