Rails - in controller, how to render an action from a different controller? - ruby-on-rails

I have a page where is displayed information about a respective car (it's a controller cars, action show). On this page, I render _form from the controller called reviews.
When I send out the form (through AJAX), the review is successfully created, but I am not able from the reviews controller render any action from the cars controller - to be more specific, here's the code:
# reviews_controller
def create
#review = Review.new(review_params)
#review.save
#respond_with(#review)
respond_to do |format|
format.js { render :action => "/cars/car_profile" }
end
end
The error I get:
ActionView::MissingTemplate (Missing template reviews/cars/car_profile, application/cars/car_profile with {:locale=>[:en], :formats=>[:js, :html], :variants=>[], :handlers=>[:erb, :builder, :raw, :ruby, :coffee, :haml, :jbuilder]}. Searched in:
* "/Users/adam/rubydev/claisy/app/views"
* "/Users/adam/.rvm/gems/ruby-2.0.0-p481/gems/devise-3.4.1/app/views"
):
app/controllers/reviews_controller.rb:28:in `block (2 levels) in create'
app/controllers/reviews_controller.rb:27:in `create'
How can I render in controller an action that is located in a different controller?
Thank you in advance.

:action forces rails to search for templates within the current controller view folder.
Missing template reviews/cars/car_profile
You should write this instead
render "cars/car_profile"
See point 2.2.3
What if you want to render a template from an entirely different
controller from the one that contains the action code? You can also do
that with render, which accepts the full path (relative to app/views)
of the template to render.

Related

Render js.erb from controller

I have a controller action that I am trying to only render js from. Here's what I have
def get_script
respond_to :js
render :script
end
I'm using respond_to :js to hopefully force the request to only respond to js. Then I'm calling render :script to load a file called script.js.erb
My request is the following
Completed 500 Internal Server Error in 15ms (ActiveRecord: 0.5ms)
ActionView::MissingTemplate (Missing template widgets/get_script, application/get_script with {:locale=>[:en], :formats=>[:js, :html], :variants=>[], :handlers=>[:erb, :builder, :raw, :ruby, :jbuilder]}. Searched in:
* "/home/jkoehms/TECC/tecc/app/views"
* "/home/jkoehms/.rvm/gems/ruby-2.0.0-p643/gems/devise-3.5.1/app/views"
)
So there are two problems here. Although the request is processed as JS, the render is looking for js or html as indicated in the formats section. Secondly, the render is looking for get_script.js.erb when it should be looking for script.js.erb. I used the following documentation as a resource for rendering: Layouts and Rendering
Question:
1.) Does the respond_to :js do what I'm hoping it to do, or do I have to put it in a do |format| block?
2.) Why isn't render :script looking for script.js.erb?
Have you tried this inside your view?
render "widgets/script.js.erb", format: :js

Pdf creating in rails, missing template

Missing template events/index, application/index with {:locale=>[:en], :formats=>[:pdf], :variants=>[], :handlers=>[:erb, :builder, :raw, :ruby, :coffee, :haml]}. Searched in: * "c:/Put/Team/project/app/views" * "c:/Put/Team/project/vendor/bundle/ruby/2.0.0/gems/browserlog-0.0.2/app/views" * "c:/Put/Team/project/vendor/bundle/ruby/2.0.0/gems/devise-3.4.1/app/views"
Here is the error I encounter when I'm trying to create a pdf from the ruby application I have made. This error occurs when I put .pdf on the end of my URL.
This is the code I've implemented in my app controller file
def show
#event = current_user.events.find(params[:id])
respond_to do |format|
format.html
format.pdf do
pdf.text = Prawn::Document.new
pdf.text "Hello World"
send_data pdf.render
end
end
end
It wants to render a template in your views, so my guess it's not recognizing your PDF render statement. It is odd that the pointer is to events/index while you're saying the source is the show method.
Try referring to this StackOverflow thread:
How to render format in pdf in rails
Make sure you name your view to "show.pdf".

.json.rabl not showing in view for Rails app

Trying to get User model to print to JSON, tried making a file with a json.rabl extension as follows:
file.json.rabl
object #users
attributes :id, :name
When I try to view this, I get the following error message:
Missing template users/autocomplete, application/autocomplete with {:locale=>[:en], :formats=>[:html], :handlers=>[:erb, :builder, :raw, :ruby, :jbuilder, :coffee, :rabl, :rb]}.
However, if I rename the file to file.rabl, it renders the JSON, but in an HTML page instead. How do I get it to print a pure JSON file?
Here is my controller code for the file view:
def file
#users = User.all
respond_to do |format|
format.html
format.json
end
Just had to add .json to the URL.

How do you disable needing a template for a respond_to in Ruby on Rails?

I am getting the following error:
ActionView::MissingTemplate (Missing template product/create, application/create with {:locale=>[:en], :formats=>[:js, :html], :handlers=>[:erb, :builder, :haml]}
controller code:
def create
product = Product.new(params[:product])
product.save
end
The reason why I'm getting this is because I don't have a respond_to block for this action or a template file. It is an create action that I am running via AJAX to create a product. I don't need/want to respond with anything after this is sent to the server. Is there a way to disable this so I don't get these errors? They best way I can think of is to create a respond_to block with format.js and create a _create.js.erb file that is blank but that seems like a hack.
Thanks
Just add
render nothing: true
http://guides.rubyonrails.org/layouts_and_rendering.html#rendering-nothing

Rendering an action outside the current view folder

I am using Ruby on Rails 3.0.9 and jQuery 1.6.2. I would like to "render an action file" from a folder not in the current view file used\displayed.
In my articles_controller file I have:
respond_to do |format|
format.html {
# ...
}
format.js {
render :action => 'shared/article.js.erb' # Note the 'shared' folder
}
end
Making an AJAX HTTP request from the article view (the view is the show.html.erb file and it is located in views/articles folder) I get the following error:
ActionView::MissingTemplate (Missing template articles/article with {:handlers=>[:erb, :rjs, :builder, :rhtml, :rxml], :formats=>[:js, :html], :locale=>[:en, :en]}...
How can I solve that?
The log file is the following:
Started GET "/articles/2/article" for 127.0.0.1 at 2011-07-24 13:05:16 +0200
Processing by ArticlesController#article as JS
ActionView::MissingTemplate (Missing template ActionView::MissingTemplate (Missing template articles/article with {:handlers=>[:erb, :rjs, :builder, :rhtml, :rxml], :formats=>[:js, :html], :locale=>[:en, :en]}...
On Rails 3, this should work:
render 'shared/article'
If it does not, you can be explicit:
render :template => 'shared/article'
More about this on the rendering chapter at Rails Guides.

Resources