Rails: Render Doesn't Work, Still Get 'Template Is Missing'

Rails: render doesn't work, still get `Template is missing`

In the render method, plain option was added in Rails 4.1 and you are using Rails 4.0.4. So, rails ignored this option and started looking for a template named articles/create as you are in ArticlesController#create action. Obviously, the template doesn't exist so you get the error Template is missing.

Refer to the discussion on this topic on Github: Introduce render :plain and render :html, make render :body as an alias to render :text

Now, for using the below mentioned syntax you would need to upgrade to Rails 4.1:

render plain: params[:article].inspect

With your current version of Rails 4.0.4, you can go for:

render text: params[:article].inspect

Getting Template is missing error when attempting to render js.erb file

Change your controller action to handle the type of request.

def show_user_issues
@target_div = params[:target_div] || "holding_issues_list"
user = User.find(params[:user_id])
issue_type = params[:issue_type]

#snip - set the value of @issues

respond_to do |format|
format.js
end
end

This will check the format of the request which is .js in case of :remote => true. So it will handle it by rendering the show_user_issues.js.erb file.

How to fix a missing template error?

If @announcement fails to save in AnnouncementsController#create, you render the default view for the action - in this case create.html.erb which doesn't exist. Thus, you get the error you're seeing.

Usually, the create action would re-render the template with the form. Usually that's the new action, which would mean adding render "new" to the else part of your create action.

In your case however, it seems like it's in the "shared/announcements_form" partial, which would mean adding

render "shared/announcements_form"

to your create action.

Missing Template error if f.collection option is not selected

Check what the action does in your controller, may you are redirecting you user for a path with no have template, this error is the correct.

Otherwise you can check if your form is sending the correct HTTP method, look eht example:

The path:

POST http://url.com/resources 

Will send you user to #create method (usually)

The same path
GET http://url.com/resouces
Will send you user to a #index, needs a template to render

OBS: I'am new in the platform, can't comment yet, try to leave a description of your error.

UPDATE

The error (Missing Template) is because yout controller is trying to render new and you don't have a jobs.new.html.erb file.

For solve the required change your collection to this:

<%= f.collection_select(:city_id, City.all, :id, :name, {:prompt => 'Choose a city'}, {:required => true}) %> 

how to fix missing template , application/create in rails

This error comes if you're hitting the create action using HTML. create doesn't typically have an associated view; you use it to process an entity, and redirect the user elsewhere.

Therefore, you should just be able to use:

redirect_to @listing

at the end of your controller code.

Using @listing is a bit of Rails magic - it would more commonly appear as redirect_to listing_path(@listing).

I.E.

  def create
@listing = Listing.new(listing_params)
if @listing.save
if params[:images]
params[:images].each { |image|
@listing.pictures.create(image: image)
}
end
(@users - [current_user]).each do |user|
Notification.create(recipient: user, actor: current_user, action: "posted", notifiable: @listing)
end
flash[:notice]= "L'annonce #{@listing.listing_number} a eté publiee avec succès."
redirect_to @listing
end
end

Does that do it?

Another common practice is to have different approaches depending on whether or not an object successfully saves to the db. For example:

def create
@listing = Listing.new(listing_params)
if @listing.save
...
redirect_to @listing, notice: "..."
else
flash.now[:alert] = "Listing failed to save"
render :new
end
end

A good way to play about with this is to use the generator to see how Rails handles things by default - you can use the following in the terminal to have a dig around: rails g controller test_controller.

Hope that helps - let me know if you've any questions.



Related Topics



Leave a reply



Submit