1

I have added a custom action in my controller called transplant. I simply want to render a dropdown form to select where to be located based on the 'tray_id'

my routes look like this:

resources :plants do
    member do
      get 'transplant'
    end
    resources :plantdats, :plant_cycles, :tasks
 end

My controller looks like this:

before_action :set_plant, only: [:show, :edit, :update, :destroy, :transplant]

def transplant

  if @plant.update(plant_params)
    redirect_to @plant
    flash[:success] = "Transplanted successfully"
  end
end
def set_plant
    @plant = Plant.find(params[:id])
end

    # Never trust parameters from the scary internet, only allow the white list through.
def plant_params
   params.require(:plant).permit(:title, :notes, :category_id, :tray_id, images_files: [])
end

Here is my button calling the action

<%= link_to 'TRANSPLANT', transplant_plant_path(@plant), class: "btn btn-raised btn-info hoverable" %>

Here is my transplant page _transplant.html.erb

<div class="row">
  <div class="col-md-8 col-md-offset-2">
    <div class="jumbotron">
      <%= form_for(@plant, html: {class: 'form-horizontal'}) do |f| %>
          <% if @plant.errors.any? %>
              <div id="error_explanation">
                <h2><%= pluralize(@plant.errors.count, "error") %> prohibited this grow from being saved:</h2>

                <ul>
                  <% @plant.errors.full_messages.each do |message| %>
                      <li><%= message %></li>
                  <% end %>
                </ul>
              </div>
          <% end %>
          <%= f.label 'NAME' %>
          <%= f.hidden_field :tray_id, value: params[:tray_id] %>
          <% if params[:tray_id].nil? %>
              <%= f.collection_select(:tray_id, Tray.all, :id, :title) %></br></br>
          <% end %>

          <%= f.submit class: 'btn btn-raised hoverable btn-success' %>
      <% end %>
    </div>
  </div>
</div>

EDIT After implementing the route to post 'transplant and changing my link code to

<%= link_to "TRANSPLANT", transplant_plant_path(@plant, tray_id: @plant.tray_id), method: "post", class: "btn btn-raised hoverable" %>

I still get the same error. It points right to the plant_params code in my controller.

These are the params that are being passed:

{"_method"=>"post",
 "authenticity_token"=>"fhSKt2DpgTwt1J4HsoBqYFSs0B9+pgSvDDxrS/u6yo4c3gvSxYlrrFDmhbPXq+cMho/eTHY+194WZ8zpcb1txA==",
 "id"=>"1",
 "format"=>"1"}

Im simply trying to update the :tray_id

Ive been at this all day, can anyone help with the error that Im getting?

mGarsteck
  • 671
  • 2
  • 6
  • 16
  • Please show the log for the action, including all parameters sent. – Michael Gaskill May 13 '16 at 01:42
  • Please also chose the code for the transplant action and view. – Michael Gaskill May 13 '16 at 01:45
  • Thought I added that part of the code, guess i slipped on that :/ – mGarsteck May 13 '16 at 02:17
  • 1
    You're trying to implement an action that requires form parameters, but you can't actually do that with a link_to action: See this answer to [Using Rails link_to for links that post](http://stackoverflow.com/a/13414818/6263819) for more information. – Michael Gaskill May 13 '16 at 02:31
  • @MichaelGaskill This actually helped. Thanks for the answer. I just rendered the form in the view and it works great now. Thanks a ton. – mGarsteck May 13 '16 at 02:34
  • 1
    You have a couple of options: a) build a complete form solution, 2) create a form with only hidden fields and have a click handler for the link that submits the form, but still looks like just a link. Option (a) is straightforward, but might not fit the UI that you want. Option (b) is more "interesting", and requires some Javascript coding. I can help with (b) if that's what you're leaning toward. – Michael Gaskill May 13 '16 at 02:38
  • @MichaelGaskill thanks for the offer and the answer. I believe its better for the ui in my app to render the form. It checks out and works exactly how I want it to now. – mGarsteck May 13 '16 at 03:00

3 Answers3

2

You should probably provide your code for your transplant action and view. Based on what you provided, it seems like you're trying to build a link, which changes the tray of a plant when clicked. If that's the case, transplant should probably be a POST route instead of GET. Also, you probably want to provide the tray_id in your post link like this:

<%= link_to "TRANSPLANT", transplant_plant_path(@plant, tray_id: {{your id}}), method: "post", class: "..." %>

Then you can get tray_id in your transplant through params[:tray_id] and re-associate your plant and tray

Zino
  • 21
  • 3
1

Essentially what I was trying to do was not easily done and my approach needed to change. I simply rendered the transplant form in my view and it works fine now. Thanks again :)

mGarsteck
  • 671
  • 2
  • 6
  • 16
0

Check your routes you just have defined get route and your request is post after the form submission

ashishmohite
  • 1,120
  • 6
  • 14