Rails 5.0.0.1 namspaced controller and render partial. bug? - ruby-on-rails

I'm having an issue with render partial: from within a namespaced controller. I should be able to render any partial from the views relative to the views folder, however, I'm unable to do so. It appends the partial path with the namespace, regardless of whatever I do. This controller responds to ajax calls and returns html.
Expected
1) render partial: "foos/bar" => "app/views/foos/_bar.html.haml"
2) render partial: "/foos/bar" => "app/views/foos/_bar.html.haml"
Getting
1) render partial: "foos/bar" => "app/views/ajax_api/foos/_bar.html.haml"
2) render partial: "/foos/bar" => "app/views/ajax_api//foos/_bar.html.haml"
note on 2 extra "/"
It does not matter what I have tried the result is
ActionView::Template::Error:
Missing partial 'insert above output'
Routes
namespace :ajax_api do
resource :setup, only: [] do
post :selection, on: :member
post :deployment, on: :member
end
end

So it appears that namespaces take priority over a defined route, always
However seems an option was added to turn off prefixing of namespace.
see: render partial of another namespace
I think I'm going to refactor my routes, to not use a namespace.
Discussion
A leading slash in the defined route should always take precedence over a namespace?

Related

link_to, view, controller

I implemented this link:
View:
<li><%= link_to "Trainer-Sportler", :controller => "trainerones", :action => "trspmatch" %></li>
controller:
def trspmatch
render :trspmatch
end
and one view trspmatch.html.erb
Rails says:
Unknown action
The action 'show' could not be found for TraineronesController
When i implemented def show end and create a show.html.erb. Rails opens the show.html.erb not the trspmatch.html.erb?
You probably want to define your trainerones resource like this:
resources :trainerones do
collection do
get 'trspmatch'
end
end
that will expose a url /trainerones/trspmatch that maps to TraineronesController#trspmatch. It seems that a look at this link will help you understand routes better.
In any case, you will benefit a lot from running rake routes in your console, which will display all your routes and how they map to your controllers methods. Try before and after rewriting your trainerones resource as I explained above, and you'll see the difference. Good luck!

Rails: link_to calls custom method in controller

I am looking to use link_to to call a method in my controller. However, for some odd reason the route looks for the show method.
In my view:
<% #beverages.each do |beverage| %>
..
<%= link_to 'Archive', beverages_archive_path(:id => beverage.id) %>
..
<% end %>
In my config/routes.rb
match 'beverages/archive' => 'beverages#archive'
In my beverages_controller.rb
def archive
beverage = Beverage.find(params[:id])
respond_to do |format|
# format.html # show.html.erb
format.json { render json: beverage }
end
# beverage.update_attribute('archive', true)
end
When I click on the archive link in the view, the URL does change to: http://localhost:3000/beverages/archive?id=11, however I get the following error.
The error I get:
ActiveRecord::RecordNotFound (Couldn't find Beverage with id=archive):
app/controllers/beverages_controller.rb:46:in `show'
Any idea on what I am doing wrong? Your help is much appreciated!
PS. I also looked at Rails 3 link_to delete destory method calls show method?
but nothing seemed to work.
Have you tried this in your routes?
match 'beverages/:id/archive' => 'beverages#archive', as: :beverages_archive
This will create the beverages_archive_path method for you. Also, as you are looking for a specific beverage, use :id inside the route so that it knows where to take the id parameter from.
Apart from that, you can always tell a link specifically which controller and action to link to by doing:
link_to "Label", :controller => :my_controller, :action => :index
Taken from here: Ruby on rails 3 link_to controller and action
Use the other notation (not match) instead.
resources :beverages do
collection do
get :archive
end
end
Try this one out and let me know if something went wrong.
There's not quite enough information here to know why beverages_archive_path works in your app -- one problem is that your routes file does not define a name for your beverages#archive route. What you want is something like:
match 'beverages/archive' => 'beverages#archive', :as => :beverages_archive
or better yet use resourceful routing conventions like so:
resources :beverages do
collection do
get :archive
end
end
What's happening is that you have a beverages#show route that matches /beverages/:id, and this is what /beverages/archive matches (with :id => 'archive').

Rails - Add action to controller created by scaffold

I'm trying to add an action called rollback to controller.
As I've seen, the only things I should do is writting the new action:
def rollback
puts "ROLLBACK!"
respond_to do |format|
format.html # index.html.erb
format.json { render json: #components }
end
Modify the routes.rb file:
resources :components do
collection do
post :rollback, :as => 'rollback'
end
end
And calling the action from some view:
<%= link_to 'Rollback', rollback_components_path %>
But I get the following error:
Couldn't find Component with id=rollback
app/controllers/components_controller.rb:18:in `show'
That's because instead of going to rollback action, the controller thinks that we are trying to 'show' to component with id 'rollback'.
Something that it seems weird for me is that calling 'new' action rails uses new_component_path (without s, in singular), but if I write rollback_component_path it throws me an error and I cant see the view.
In your routes you require a POST, just clicking a link is by default a GET, so either write
resources :components do
collection do
get :rollback
end
end
and then the link_to will work as expected.
I am assuming the rollback operation is not idempotent, so a POST is semantically better in that case.
If you write your link as follows, then rails will create an inline form for you:
link_to 'Rollback', rollback_components_path, :method => 'post'
Hope this helps.
This will work
routes.rb
resources :components
match "components/rollback" => "components#rollback", :as => :rollback
In views
<%=link_to 'Rollback', rollback_path%>

Rendering partials not working from controller

I've got a home_controller.rb that has different methods whose purpose is to just render render partials that have different content.
I have the urls created in the config
get '/interview', :to => 'home#interview'
get '/chambers', :to => 'home#chambers'
get '/letter', :to => 'home#letter'
get '/drafting', :to => 'home#letter'
and the methods set up that render partials (I was experimenting with symbols vs single quotes) from the home_controller.rb
def chambers
render 'home/chambers'
end
def drafting
render 'drafting'
end
def interview
render :interview
end
However, I'm getting a missing template error (Template is Missing) message when I click the links.
I can get it to work without using partials but rather regular files interview.html.erb (for example) but I would still like to know why it's not working with partials.
Thanks
you need to specify it as partial if you want to render a partial else rails will look for file or an action with that name.
render :partial => 'drafting'
Code for rendering logic in rails https://github.com/rails/rails/blob/5215eed5a3f18c76d70f0f25bca4ff6286c4bac8/actionpack/lib/abstract_controller/rendering.rb#L141

Ruby on Rails form_remote_tag missing template

I'm using form_remote_tag(:url => {:controller => "home", :action => "search"}, :update => "mydiv"). When I click submit on the form "mydiv" is populated with the error "Template is missing. Missing template home/search.erb in view path app/views". I've tried multiple render options in def search, but they all result in the same error.
It looks like the search method is trying to use it's default render even though I'm specifying what I want.
I've tried:
render 'index'
render :text => 'Return this from my method!'
Is my url incorrect? Is it not submitting back to my home controller's search method?
Try
render :action => 'index'
this will use "index.rhtml" or "index.html.erb".
I will try to explain why it said search.erb is not found, lets take create action for a some model, if there is some error in my create action they it will throw missing template create.html.erb file, since you have some error in your create action rails will try to render the create.html.erb in the page. Hope I explained it clearly.
In an ajax action you can't use redirect_to or render options directly.
try using this in your search action
render :update do |page|
page.replace_html "ur_div_id","partial"
end
The form_remote_tag needs prototype to function. Make sure you are including the :defaults for your javascript libraries namely prototype.
<%= javascript_include_tag :defaults %>

Resources