I am new to rails. Currently trying to pull from an api and store the value of each api request into a form for entry into my DB.
My page has a search bar at the top which pulls the form the api and returns the value of each result to the screen with a button prompting to add to the DB.
<ul>
<% #movies.each do |movie| %>
<li><%= movie['Title'] %> | <%= movie['Year'] %> |
<%= form_tag(movies_path, :method => "post") do %> <%=hidden_field :movie, :title, :value => movie["Title"] %> <%= hidden_field :movie, :year, :value => movie["Year"] %> <%= hidden_field :movie, :imdb_id, :value => movie['imdbID'] %> <%= submit_tag "Add Movie", :name => nil %> <% end %> </li><br>
<% end %>
</ul>
My controller for both my create a #movie for the index/ search results page is this
class MoviesController < ApplicationController
def index
if params[:search].present?
#movies = Omdbapi.search(params[:search])
else
render :'/watchlists'
end
end
def show
#movie = Omdbapi.more_info(params[:imdb_id])
end
def create
#movie = Movie.new(movie_params)
if #movie.save
redirect_to movies_path(#movie)
end
end
When I try to hit submit rails throw this error
Missing template movies/create, application/create with {:locale=>[:en], :formats=>[:html], :variants=>[], :handlers=>[:erb, :builder, :raw, :ruby, :coffee, :jbuilder]}.
Any help would be awesome.
#movie did not persisted for some reason so that redirect_to hasn't been performed. Add else statement to condition:
if #movie.save
redirect_to movies_path(#movie)
else
render action: 'new'
end
Related
This question already has answers here:
Rails “Template is missing” error
(2 answers)
Closed 5 years ago.
I am beginner in rails
In routes.rb I have
root 'post#index'
resources :posts
When I click on "new post" for the empty title of the post,I get a error like
Missing template posts/new, application/new with {:locale=>[:en], :formats=>[:html], :variants=>[], :handlers=>[:erb, :builder, :raw, :ruby, :coffee, :jbuilder]}. Searched in: * "/home/supranimbus12/RoR/instagram_app/app/views" * "/home/supranimbus12/.rvm/rubies/ruby-2.3.0/lib/ruby/gems/2.3.0/gems/devise-4.4.0/app/views"
My posts/new.html.erb file like
<h2>New Post</h2>
<hr>
<%= form_for #post do |f| %>
<% f.label :description %>
<% f.textarea :description %>
<hr>
<% f.submit %>
<% end %>
1- change routes as: -
root 'post#index'
resources :posts, except: [:index]
because you have already defined as root of index action
2- now for for new action
def new
#post = Post.new
end
3- in you app/view/posts/new.html.erb
<h2>New Post</h2>
<hr>
<%= form_for #post do |f| %>
<% f.label :description %>
<% f.textarea :description %>
<hr>
<% f.submit %>
<% end %>
3 - after hitting the submit button it will go to create action,
note: - because form_for object automatic hit to create action if object is new otherwise it will hit the update action that's why we generally use this form as partial to use it for edit and new action both
def create
#post = Post.new(post_params)
if #post.save
flash[:notice] = "post saved successfully!"
redirect_to #post
else
flash[:error] = #post.errors.full_messages.to_sentence
render 'new'
end
private
def post_params
params.require(:post).permit!
end
to permit all data make a private method for strong parameter
post_params
and here redirect_to #post will go to show action if post saved in db,as it will make url like post/:id otherwise it will render to new template again. with error flash message.
I am trying to recover the error message of the validations on my form (there must be no duplicate of ingredient) but I always have a MISSING TEMPLATE that I can not correct, I know that It's a routes problem, but I do not know which one.
I have 3 models, Cocktail Ingrédient and Dose, Dose link Cocktail and Ingredient
ERROR MESSAGE
MISSING TEMPLATE
Missing template cocktails/23 with {:locale=>[:en], :formats=>[:html], :variants=>[], :handlers=>[:raw, :erb, :html, :builder, :ruby, :coffee, :jbuilder]}. Searched in: * "/home/dezrt/code/Pseud0/rails-mister-cocktail/app/views"
doses_controller.rb
class DosesController < ApplicationController
before_action :set_dose, only: [:show, :destroy, :edit]
def index
#doses = Dose.all
end
def show
end
def new
#dose = Dose.new
end
def create
#dose = Dose.create(dose_params)
#cocktail = Cocktail.find(params[:cocktail_id].to_i)
#dose.cocktail = #cocktail
if #dose.save
redirect_to cocktail_path(#dose.cocktail)
else
# params[:dose][:cocktail_id] = #cocktail.id.to_s
#ingredient = Ingredient.find(params[:dose][:ingredient_id].to_i)
render cocktail_path(#cocktail)
end
end
def edit
end
def destroy
#dose.destroy
redirect_to cocktail_path(#dose.cocktail)
end
private
def dose_params
params.require(:dose).permit(:cocktail_id, :ingredient_id, :quantity, :description)
end
def set_dose
#dose = Dose.find(params[:id])
end
end
cocktails_contronller.rb
class CocktailsController < ApplicationController
before_action :set_cocktail, only: [:show]
def index
#cocktails = Cocktail.all
end
def show
#cocktail = Cocktail.find(params[:id])
#dose = Dose.new
#dose.cocktail = Cocktail.find(params[:id])
#ingredient = Ingredient.new
#ingredients = Ingredient.all
end
def new
#cocktail = Cocktail.new
end
def create
#cocktail = Cocktail.create(cocktail_params)
if #cocktail.save
redirect_to cocktail_path(#cocktail)
else
#cocktail = Cocktail.new(cocktail_params)
render :new
end
end
private
def cocktail_params
params.require(:cocktail).permit(:name)
end
def set_cocktail
#cocktail = Cocktail.find(params[:id])
end
end
views/cocktails/show.html.erb
<h1>Cocktail X</h1>
<h2>Voici la listes de tout nos cocktails</h2>
<div class="container">
<h3>Nom du cocktail : <%= #cocktail.name %></h3>
<ul>
<h4>Ingredients : <% #cocktail.doses.each do |dose| %></h4>
<li><%= dose.ingredient.name %> : (<%= dose.quantity %><%= dose.description %>)</li>
<%= link_to dose_path(dose), method: :delete do %>
<i class="fa fa-close"></i>
<% end %>
<% end %>
</ul>
</div>
<%= link_to "Retour aux cocktails", cocktails_path %>
<div class="container">
<h4>Ajouter des ingrédients</h4>
<%= simple_form_for [#cocktail, #dose] do |f| %>
<%= f.input :ingredient_id, collection: #ingredients %>
<%= f.input :quantity, label: 'Quantité', error: 'La quantitée est obligatoire' %>
<%= f.input :description, label: 'Description', error: 'La description est obligatoire' %>
<%= f.button :submit %>
<% end %>
</div>
routes.rb
Rails.application.routes.draw do
# For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html
root "cocktails#index"
resources :cocktails do
resources :doses, only: [:new, :create, :edit]
end
resources :doses, only: :destroy
end
rails routes
Prefix Verb URI Pattern Controller#Action
root GET / cocktails#index
cocktail_doses POST /cocktails/:cocktail_id/doses(.:format) doses#create
new_cocktail_dose GET /cocktails/:cocktail_id/doses/new(.:format) doses#new
edit_cocktail_dose GET /cocktails/:cocktail_id/doses/:id/edit(.:format) doses#edit
cocktails GET /cocktails(.:format) cocktails#index
POST /cocktails(.:format) cocktails#create
new_cocktail GET /cocktails/new(.:format) cocktails#new
edit_cocktail GET /cocktails/:id/edit(.:format) cocktails#edit
cocktail GET /cocktails/:id(.:format) cocktails#show
PATCH /cocktails/:id(.:format) cocktails#update
PUT /cocktails/:id(.:format) cocktails#update
DELETE /cocktails/:id(.:format) cocktails#destroy
dose DELETE /doses/:id(.:format) doses#destroy
Thanks you for your help !
Missing template cocktails/23 with {:locale=>[:en], :formats=>[:html],
:variants=>[], :handlers=>[:raw, :erb, :html, :builder, :ruby,
:coffee, :jbuilder]}. Searched in: *
"/home/dezrt/code/Pseud0/rails-mister-cocktail/app/views"
This line render cocktail_path(#cocktail) is the reason for the error. render loads a view. So the input to the render normally should be name of the file, in your case show view of the cocktails. Changing it to render 'cocktails/show' should fix the error.
Also you seems to be confused between render and redirect_to. I suggest you to read render vs redirect
I'm trying to figure out what an error message means. I am using statesman gem for states. One of my states is called 'requested'.
state :requested, initial: :true
state :approved
state :rejected
state :removed
transition from: :requested, to: [ :approved, :rejected ]
transition from: :approved, to: [ :removed ]
I have an index, listing all the states that an object can transition to:
<% if policy(orgReq).approved? %>
<%= link_to "APPROVE", requested_organisation_request_path(orgReq), :class=>'btn btn-info', method: :put %>
<% end %>
<% if policy(orgReq).rejected? %>
<%= link_to "DECLINE", decline_organisation_request_path(orgReq), :class=>'btn btn-info', method: :put %>
<% end %>
<% if policy(orgReq).removed? %>
<%= link_to "REMOVE", removed_organisation_request_path(orgReq), :class=>'btn btn-info', method: :put %>
<% end %>
</td>
I have a controller action for each state:
def requested
# this is the initial state - there is nothing in the controller action
end
def approved
organisation_request = OrganisationRequest.find(params[:id])
authorize #organisation_request
if organisation_request.state_machine.in_state?(:requested)
flash[:notice] = "You've been added as a member. Welcome aboard."
format.html { redirect_to :index }
else
flash[:error] = "You're not able to manage this organisation's members"
redirect_to(profile_path(current_user.profile))
end
end
def removed
organisation_request = OrganisationRequest.find(params[:id])
authorize #organisation_request
if organisation_request.state_machine.in_state?(:approved)
flash[:notice] = "Removed from the organisation."
format.html { redirect_to :index }
else
flash[:error] = "You're not able to manage this organisation's members"
redirect_to(profile_path(current_user.profile))
end
end
I don't understand what the error message below means? It comes when I try to click a button on the index to change the state of the object from requested to approved (which is an allowed transition).
Does anyone know how to interpret this error message? Do I need to add something to the requested method in the controller?
ActionView::MissingTemplate at /organisation_requests/1/requested
Missing template organisation_requests/requested, application/requested with {:locale=>[:en], :formats=>[:html], :variants=>[], :handlers=>[:erb, :builder, :raw, :ruby, :coffee, :haml, :jbuilder]}. Searched in:
I am using carrierwave and remotipart to handle an AJAX form where I upload an album of music and accompanying information to a website. After submitting the form, I would like to redirect the partial where the form was located to the newly created album's page. The form, located in \albums\_new.html.erb looks like this:
<h1>Add a new album</h1>
<div>
<%= form_for(album, remote: true, url: {action: "create"}, html: {multipart: true}) do |f|%>
<h2>Information: </h2>
<div>
<%= f.label :title %>
<%= f.text_field :title %>
<%= f.label :year %>
<%= f.text_field :year %>
<%= f.label :image %>
<%= f.file_field :image %>
</div>
...
<%= f.submit "Add Album" %>
<% end %>
</div>
The controller's new and create methods look like this:
def new
#album = Album.new
#album.producers.build
#album.tracks.build
end
def create
respond_to do |format|
#album = Album.new(album_params)
if #album.save
format.js {redirect_to album_path(#album)}
end
end
end
The redirect currently only works when I submit the form without any file attachments. If I do try to include a file attachment, the upload still succeeds but the redirect fails and I get the following message in my console:
ActionView::MissingTemplate (Missing template albums/show,
application/show with {:locale=>[:en], :formats=>[:html], :variants=>[],
:handlers=>[:erb, :builder, :raw, :ruby, :coffee, :jbuilder]}.
I have been able to implement a workaround where I simply call the same expression in create.js.erb that I do in show.js.erb:
$('#listen-window').html("<%= j (render partial: 'show', locals: {album: #album, tracks: #tracks, producers: #producers}) %>")
and write a create method as such:
def create
respond_to do |format|
#album = Album.new(album_params)
if #album.save
#tracks = #album.tracks
#producers = #album.producers
format.js
end
end
end
Functionally, this code does achieve what I want. However, it does not make sense to me from a routing perspective. How would you write a more elegant solution?
I'm trying to render HTML from an ERB object within a controller (the ERB object is stored in the DB), but with no success.
I would like to do something like this:
First, I created a Func instance:
Func.Create(text: "<% #users.each do |user| %>
<td><%= user.id %></td>
<td><%= link_to user.username, user_path(user) %></td>")
Then, in the controlller:
class MainController < ApplicationController
def foo
#users = User.all
render :html => bar(binding)
end
def bar(controller_binding)
html = Func.find(1)
template = ERB.new(html.text)
template.result(controller_binding)
end
end
But all I get are errors, like this:
"Missing template main/resource_name, application/resource_name with
{:locale=>[:en], :formats=>[:html], :handlers=>[:erb, :builder, :raw, :ruby,
:jbuilder, :coffee]}. Searched in: * "c:/Users/david/sample/app/views""
and
"(erb):3: syntax error, unexpected $end, expecting keyword_end ...
ut.force_encoding(__ENCODING__) ... ^"
Replace
Func.Create(text: "<% #users.each do |user| %>
<td><%= user.id %></td>
<td><%= link_to user.username, user_path(user) %></td>")
with
Func.Create(text: "<% if #users.nil? %>
<p> There are no users in database </p>
<% else %>
<% #users.each do |user| %>
<td><%= user.id %></td>
<td><%= link_to user.username, user_path(user) %></td><% end %> <% end %>")
<% end %> end of block was missing.
Also, replace render :html => bar(binding) with render :inline => bar(binding)
You could also shorten your code as below,
def foo
#users = User.all
html = Func.find(1)
render :inline => html.text
end
You shouldn't doing that in the controller. Not sure what you're trying to accomplish but it looks like a helper may be more appropriate.