I have created a new method in one of the project's controllers that it will allow the users to search using SearchLogic's gem.
The method is called search_entries and it is of course accompanied by a corresponding view. But when I hit the "Submit" button Rails complains that "Couldn't find Entry with ID=search_entries" (where Entry is the model.) In the params hash there is an ID with value "search_entries".
When I place the code from the search_enrties view inside the index template everything works without a problem (and no, the params hash does not have an ID...)
I am sure the problem is caused from a lack of understanding of how RoR works.
Thank you in advance for your time,
Angelos Arampatzis
i believe this is caused by the route entries in your config/routes.rb file as it's using RESTful actions. try to add a :search_entries => :get into the :collection of your resource.
more info can be found here: http://guides.rubyonrails.org/routing.html#adding-more-restful-actions
hope it helps =)
Related
I have tried using the Wicked gem 3 different times over the past 8 years. Each time, I have given up for the same reason. I'm trying again, because if I understand it, I think it will be perfect for my use case.
My main problem is that I don't understand how to actually begin the wizard. With the example used in the gem, it is an after_registration event that already has an associated user object. That is not helpful, nor do I think that example would be helpful in the majority of use cases.
There is another example about building a Product in multiple steps. However, the author fails to adequately explain the routing. From https://github.com/zombocom/wicked/wiki/Building-Partial-Objects-Step-by-Step:
Since Wicked uses our :id parameter we will need to have a route that also includes :product_id for instance /products/:product_id/build/:id. This is one way to generate that route:
resources :products do
resources :build, controller: 'products/build'
end
This also means to get to the create action we don't have a product_id yet so we can either create this object in another controller and redirect to the wizard, or we can use a route with a placeholder product_id such as [POST] /products/building/build in order to hit this create action.
OK, I have no idea what the second part of the sentence means as far as placeholder product_id and that route name of /products/building/build. I spent 2 hours trying that and just moved on to a blank create form.
...we can either create this object in another controller and redirect to the wizard
That's what I'm trying to do upon successful save of the #product object.
redirect_to product_by_interchange_path(#product, :step1)
That doesn't work. raise InvalidStepError if the_step.nil? Says my step is nil. It's not.
redirect_to product_by_interchange_path(#product, step: :step1)
Same thing.
redirect_to product_by_interchange_path(:step1)
That's an exact mirror of the 8 year old example app. But of course #product isn't in a session variable like current_user is, so in this case the error is that there's no Product with an id of :step1.
Please help! I am missing something very, very basic here but I very much need to persist.
OK I have finally figured this out. Here's what I did:
First of all, I changed my controller back to a plain old ApplicationController and used the include include Wicked::Wizard. I don't know if that did anything, but the newer example was laid out like the old.
I was really screwed up by :id. I'm thinking :id is generally my object ID. I had a set_product private method in my controller, and it was failing. When I finally figured out that :id was the actual step itself, that led me to change my path in the redirect.
I changed the redirect from product_by_interchange_path(#product, :select_vehicle) to product_by_interchange_path(:select_vehicle, product_id: #product.id)
I got rid of my set_product. Just while I was trying to eliminate confusion.
I changed my finder calls in the wizard to use :product_id instead of :id.
It works now. I still don't understand how I could have stubbed out a route with a placeholder product_id, that's still a mystery. But this is fine and it works.
I'm pretty new to rails, and I'm hoping you can help me understand how the following works.
At present I have a controller named projects (projects_controller.rb). From what I understand of ROR each controller has some basic (inherent) methods such as index, new, create, edit, show, etc. I would like to add a method called "help" and a view to display the help information.
At present a user can create many projects. The projects contain a set of fields that are populated by the user. I would like to add a help page that the user can access (via a link on the project screen) which explains each project field and how to best fill it out. I would like this to be an independent page (not just info displayed on the project's page).
As of now, the URL to the projects is (when editing a project): localhost:3001/projects/id/edit
I would like for the path to the help file to be localhost:3001/projects/id/help
If I want the help file to be located in the path listed above, am I correct in assuming that I need to create a new method, called "help", in the projects controller? And if so, is there something that I need to add to routes.rb to make it function? And would I use a link_to function in the Haml to create a link to it?
I'm sorry if this seems confusing or a lot of question. I appreciate your time. If you have any suggestions on whether on the right path please let me know. Thank you so much!
I think currently you have this in routes
resources :products do
end
just replace this with
resources :products do
get :help, :on => :member, :as => :help
end
And add method in controller and add view named help.erb.html(if you r using erb) in views/product folder.
you can use help_path and help_url
I am using the on_the_spot gem for editing a certain column in a rendered table. This works fine.
But when I create a new records and update the table using AJAX I get the following error message:
"ActionView::Template::Error (No route matches {:action=>"update_attribute_on_the_spot", :controller=>"stores_spare_parts"}): "
How can I solve this problem?
From the code i see no obvious error. What i guess that is wrong is that the mentioned route is indeed not defined. I would assume that was not possible, because it works in the index-view, but if the index-view belongs to a different controller, e.g. the store, and creating the store_spare_part goes to his controller, than that is a possible solution. But this is just a guess.
So do you have the following in your routes:
resources :stores_spare_parts do
collection do
post :update_attribute_on_the_spot
end
end
With the information i have now, this is the only logical explanation i can think of. I hope it helps.
[EDIT: found another workaround] I forgot, but the on_the_spot gem has the option to explicitly specify the url. The intention was to be able to supply/use your own method, but it could also be used in this case.
Usage:
on_the_spot_edit post, :title, :url => url_for({:controller => 'stores/store_spare_parts', :action => :update_attribute_on_the_spot})
It still remains weird that it should be needed, but I hope this is a valid workaround.
I have product with a foreign collection_id key
I want to pass an id to a controller.
To do so i have the following routes for my controller :
controller :magasin do
get "magasin" => "magasin#index"
end
The only view in my controller is magasin/index.html.erb
The link to magasin is link_to collection.nom, magasin_path(collection)
This kind of syntax usually works in controllers with models. Here my link is : http://localhost:3000/magasin.2 instead of http://localhost:3000/magasin/2
Later on i will need to call the same view with product_kind_id instead of collection_id and i will add sort by name/price ....
How can i have the ID as a normal argument (/:id)instead of a type(.id)?
A popular URL schema to follow is RESTful routing. Rails has this built-in and will set it up for you if you initialize your resource via rails generate scaffold Magasin nom:string.
If you put resources :magasins in your routing file, it will route /magasins to MagasinsController#index and /magasins/1 to MagasinsController#show with params[:id] set to "1". It will also set up a few other routes that will be useful in the future but for now will just raise an action not found exception.
You don't want to use the dot as an argument delimiter, since Rails places what comes after the dot in the request.format method or just in params[:format] (ordinarily accessed through the respond_to method that comes with the generated scaffolds). Save that dot for later when you are working on delivering alternative display formats like XML and JSON.
I realize I've said a lot in a small space, so feel free to ask any follow up questions once you've consulted the Rails Guide on the issue, and I'll be very glad to help!
So I generate my scaffold in rails, and it creates the usual CRUD files. In my View, I copy over the form found in new.html.erb and paste it over at index.html.erb, so I can create a new record from my index. When I do that, I get the following error consistently, no matter what I do.
Called id for nil, which would mistakenly be 4 -- if you really wanted
the id of nil, use object_id
I got tired of searching all over the web for answers, and just learned it's called a whiny nil (not much help). I tried renaming my instance variables, capitalization, using global variables, etc. but it's frustrating that Rails doesn't have an error documentation library. Can anyone help?
Do you have all the required objects present in your controller? Looks like it's calling something.id, but that something does not exist in your index action. Look at the whole error message - it should be saying what line is causing it, then check that line in source files for the missing variable.
In your controller, you need to add the code found in new into index (I guess it is something like #model = Model.new). Or better: Make a private method which has name expose_new or something like that, move the common code down there and add before_filter :expose_new, :only => [:index, :new].
Just a side note..
If I were you, I'd make a partial out of your form in new, and render that in both index and new (and quite possible edit if them all are equal) so you don't need to copy paste it in.
So, you'll end up with one _form.html.erb which includes the form, and in new and index, you have <%= render('form') %>