Rails: param is missing or the value is empty because param appearing as "#<Edge:0xb2535b8>"

82 Views Asked by At

I'm using form for and the name of the form in the controller is appearing as "#", but I expect it to appear as :edge.

Here's my controller:

  def new
      #some_stuff
      @edge = Edge.new
      #some_stuff
  end

  def create
    @edge = Edge.new(edge_params)
    @edge.save
  end

  def edge_params
    params.require(:edge).permit(:location_1_id, :location_2_id)
  end

View:

<%= form_for( :edge, :url => {:action => 'create'}) do |f| %>
  <ul>
    <li>
      <%= f.label :location_1_id %>
      <%= collection_select(@edge, :location_1_id, @location, :id, :record_as_string) %>
    </li>
    <%= submit_tag(t(:create_edge)) %>
  </ul>
<% end %>

Param req:

{"utf8"=>"✓", "authenticity_token"=>"blah", "#"=>{"location_1_id"=>"91", "location_2_id"=>"92"}, "commit"=>"Create Edge", "action"=>"create", "controller"=>"admin/edges", "floor_map_floor_id"=>"1"}

So the name of the parameter should be :edge but it's an object that I can't access.

Can anybody tell me what I'm missing? Any help is appreciated.

1

There are 1 best solutions below

2
On BEST ANSWER

You should be using the form block on your collection set like so

<%= f.collection_select(:location_1_id, @location, :id, :record_as_string) %>

(notice that you are calling the collection_select on the block variable f, and NOT passing in the @edge as the first argument).

In addition, because you are creating the object in new (@edge = Edge.new), you should just be using in your form, like so

<%= form_for( @edge, :url => {:action => 'create'}) do |f| %>

(although using :edge wasn't the cause of your problems, I suspect it was because you were using :edge and @edge in the same form. You need to be consistent, use one or the other)