Вопрос

I don't quite get why this is happening, maybe someone of you does... here it goes.

I've created a nested resource:

resources :order do
  resources :ordered_vehicles
end

I've added a link_to the new action and passed the order.id like so new_order_ordered_vehicle_path(order.id) the page is loaded nicely. The problem is after I press the button to submit the choice. He switches the path from http://localhost:3000/order/3/ordered_vehicles/new to http://localhost:3000/order/R076027535/ordered_vehicles and displays error Couldn't find Order with id=R076027535... go figure.

The error is being raised in the controller in this method

private
  def find_order
    @order = Order.find(params[:order_id])
  end

Which is a before_filter.

the new.html.haml file looks like this

= form_for [@order, @ordered_vehicle], html: { multipart: true } do |f|
  = @order.number
  %br= @order.id
    = f.fields_for :vehicles do |car|
      .... #some fields here
      = car.submit "Save your choice"

That id he can't find is the @order.number but I don't get why the switch.

EDIT: Just to be thorough, I'll add the controller methods:

def new
  @ordered_vehicle = @order.ordered_vehicles.build(params.slice(:order_id, :vehicle_id))
end

def create
  binding.pry
  @ordered_vehicle = @order.ordered_vehicles.build(params.slice(:order_id, :vehicle_id))
  if @ordered_vehicle.save
    flash[:notice] = "Save successful."
    redirect_to account_path
  end
end

POST request (I hope that's the one, still new to all this stuff):

"action_dispatch.request.formats"=>[text/html]},
 @request_method="POST", @filtered_parameters={"utf8"=>"✓", "authenticity_token"=>
 "Ar4vy8pqCSpA2ch0qG0qiJXAJUbNALYxm/FbuKbdzCc=", "ordered_vehicle"=>
 {"vehicles"=>   {"maker_id"=>"", "model_id"=>"", "year"=>"", "body"=>"", "capacity"=>"", "id"=>"1"}},
 "commit"=>"Save your choice", "action"=>"create",
 "controller"=>"spree/ordered_vehicles", "order_id"=>"R076027535"}, @method="POST",
 @fullpath="/order/R076027535/ordered_vehicles">
Это было полезно?

Решение

As per request ;)

Well, it turned out to be a problem with Spree which I'm currently tweaking (I know I didn't mention it explicitly, but didn't want to just post too much information).

Bottom line:

In the Order model the method to_param was overwritten to pass the number column in to the params. Didn't overwrote it again, just left it there and adapted. In my find_order method I wrote:

  def find_order
    @order = Order.find_by_number(params[:order_id])
  end

Also I've stored the order number in the table, there was a problem I believe with out that, but can't remember explicitly. Anyways, thanks for the help.

P.S. Sorry for the mess

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top