I am getting the error:
Template is missing
Missing template customers/search, application/search with {:locale=>[:en], :formats=>[:html], :variants=>[], :handlers=>[:erb, :builder, :raw, :ruby, :coffee, :jbuilder]}.
This is happening when I'm trying to make a search bar to search through my existing customers in the database. I think it's happening because I am using a partial '_search.html.erb' but it needs to be a partial and I don't know how to fix this problem.
Here is my customers\ _search.html.erb:
<%= form_tag search_customers_path do %>
<input class = "searchbar" id ="custsearch" name=query" placeholder = "find colleague" type="text">
<% end %>
the html it's being rendered with (in events\new.html.erb):
<div class = "searchbar">
<%= render partial: 'customers/search', :object => #customers, locals:{} %>
</div>
here is my customers controller 'search' method:
def search
#q = "%#{params[:query]}%"
#customers = Customer.where("first_name LIKE ? or last_name LIKE ? ",#q,#q)
render :layout => false
end
and here is my routes file:
root 'pages#home'
get '/main' => 'pages#main'
get '/signup' => 'customers#new'
resources :customers do
collection do
get 'search'
end
end
get '/compose' => 'events#new'
resources :events
I'm not even sure if this search will work, but this is the first hurdle to achieving it. Please help!
Thanks
Your search action looking for customers/search.html.erb but you don't have any. Try create a new file in customers folder with name search.html.erb and paste your below code there:
<div class = "searchbar">
<%= render partial: 'customers/search', :object => #customers, locals:{} %>
</div>
also remove render :layout => false from your search action. If you are sending request in html format.
#punitcse's answer was right:
def create
# this will be looking for customers/search.html.erb
# you have to explicitly define it:
render partial: "search", layout: false
end
This should resolve the error.
Calling the partial in the view is pretty normal:
<%= render partial: "customers/search", object: #object %>
The issue looks like it's caused by your create action, which can be corrected to resolve it.
You just need to modify you controller to something like
def search
#q = "%#{params[:query]}%"
#customers = Customer.where("first_name LIKE ? or last_name LIKE ? ",#q,#q)
render partial: "customers/search", locals: { customers: #customers }, :layout => false
end
In cutomers/_search change to
#customers/_search.html.erb
<%= form_tag search_customers_path, method: :get do %>
<input class = "searchbar" id ="custsearch" name=query" placeholder = "find colleague" type="text">
Related
I'm trying to write an edit action for a my post object which is nested inside the projects one. By the looks of it I'm doing everything as it should be but for some reason when I comes down to load the form, this error message appears "First argument in form cannot contain nil or be empty"
The params being sent are correct "project_id"=>"308", "id"=>"41", however it seems like the form does not know what to do with them.
form
.....
<div class="row">
<div class="col-md-12" id="postform">
<%= form_for ([#project, #post]), html: { multipart: true } do |f| %>
....
view
....
<% if current_user?(post.user) %>
<%= link_to edit_project_post_path(#project, post ) do %>
....
controller
....
before_action :logged_in_user, only: [:create, :edit]
def edit
#project = Project.find(params[:project_id])
#post = #project.posts.find(params[:id])
end
....
post is a nested object of projects and so it has been config on the route s file.
Any ideas what might be causing this problem?
I thought that maybe I need to specify on the form what are these values used for...
Thanks for the help
---- Edit ----
resources :projects do
resources :comments,:posts
end
resources :posts, only: [:create, :destroy, :edit]
form_for is not supposed to work with multipart: true, try removing that or try with form_tag.
Best regards.
I was following the instructions on the wiki for this gem:
https://github.com/mislav/will_paginate/wiki
And it works great for my listings controller :/ I was able to get them paginate but I have no idea how to get it work with my comments. My comments controller doesn't have a show method.
class CommentsController < ApplicationController
before_action :authenticate_user!, :except => [:show]
def create
#comment = Comment.new(params[comment_params])
#comment.listing_id = params[:listing_id]
#comment.user_id = current_user.id
#comment.body = params[:comment][:body]
if #comment.save
flash[:success] = "Comment Successful."
redirect_to listing_path(#comment.listing)
else
flash[:alert] = "Comment Failed."
end
end
def destroy
#comment = #listing.comments.find(params[:id])
#comment.destroy
flash[:success] = "Comment Removed."
redirect_to listing_path(#listing)
end
private
def comment_params
params.require(:comment).permit(:user, :body)
end
end
This is the show method I have inside my ListingsController:
def show
#comment = Comment.new
#comment.listing_id = #listing_id
end
It's what sets the comments to my understanding
This is the listings/show.html.erb file's part where it renders the comments:
<div class="commentblock">
<div class="text-left">
<%= render partial: 'comments/form' %>
<%= render partial: 'listings/comment', collection: #listing.comments.reverse %>
</div>
</div>
I know the first part of ruby code actually renders the forms but I don't understand how or what the collection part works or how to apply the pagination to this.
The listings/comment file looks like this:
<div class="panel panel-default">
<div class="panel-heading"><%= comment.user.username %>: <%= time_ago_in_words(comment.created_at)%> ago</div>
<div class="panel-body">
<%= comment.body %>
</div>
</div>
I've tried quite a few things but I still don't really get where or what to change in my code. I went into the listing/comment and tried adding:
<%= will_paginate(comment) %> # this crashed, undefined method `total_pages' for #<Comment:0x007ff556a36468>
EDIT: The will_paginate wiki says you to pass a paginated array when you get the error I got, but I don't know that is done >.>; it doesn't show how you would do that...
EDIT:
http://csnipp.com/s/709 this website said all you had to do was create a file config/initializers/will_paginate.rb and place this line of code in it:
require 'will_paginate/array'
But that didn't help
EDIT:
If you guys need to see more of the code it's actually on github:
https://github.com/ilovemysillybanana/pastie/
I'm really stuck on this D: I followed a tutorial on how to make the comments but theirs didn't paginate.
EDIT:
I fixed it!
The way to do it was by replacing this line:
<%= render partial: 'listings/comment', collection: #listing.comments.reverse %>
with this line:
<%= render partial: 'listings/comment', collection: #list_comments = #listing.comments.paginate(:page => 1, :per_page => 5) %>
But for some reason I don't get the numbers to change the comment pages :/
<%= render partial: 'listings/comment', collection: #listings = #listing.comments.reverse.paginate(:page => params[:page], :per_page => 10) %>
My code was correct, what I wasn't realizing is that #listings or #list_comments was creating a local variable that needed to be paginated itself. I was trying to find a way to paginate either in one command(which if possible I don't care, but if you know how it'd be nice to know for the future I guess) anyway, all that I needed to do to fix this was add this:
<%= will_paginate #listing_comments %>
I'm trying to write an application that will have to interact with POST but I'm having issues accessing the parameters. This is not meant to be useful right now but I'm just trying to flash the result of the form. According to google dev tools, the POST parameter is set to 1.
Here's my routes.rb file
devise_scope :user do
put 'users/toggle_activation' => 'users/sessions#toggle_activation'
get 'users/sign_out' => 'users/sessions#destroy'
post 'pages/home' => 'users/sessions#activities'
end
This is the controller in question
def activities
params.permit(:tennis)
current_user.save
flash[:notice] = params[:tennis]
redirect_to root_path
end
This is the form code
<%= form_for :user do |f| %>
<%= f.check_box :tennis %>
<%= f.submit %>
<% end %>
Any help is appreciated.
If you're using "form_for", then the check_box name will result as "user[tennis]", not just "tennis". View source in your browser and you should see this.
Do something like the following in your controller method (although I'm not sure how it will be called with "form_for :user" because your "activities" route isn't in your routes.rb in the code above):
user_params = params.require(:user).permit(:tennis)
flash[:notice] = user_params[:tennis]
I've been staring at this for a while and Google hasn't helped much so I'm turning to you guys for help. This should be a pretty simple fix.
The goal of the code is to take an email address from a sign up field and place it in the database.
I think most of what I need is there but I'm getting this error:
undefined method model_name for NilClass:Class
My home.html.erb file contains the following:
<%= form_for(#signup) do |f| %>
<div class="field">
<%= f.label :email %><br />
<%= f.text_field :email %>
</div>
<div class="actions">
<%= f.submit "Enter" %>
</div>
<% end %>
The model contains this:
class Signup < ActiveRecord::Base
attr_accessible :email
email_regex = /\A[\w+\-.]+#[a-z\d\-.]+\.[a-z]+\z/i
validates(:email, :presence => true,
:length => {:maxiumum => 40},
:format => {:with => email_regex})
end
The controller contains:
class SignupController < ApplicationController
def show
end
def new
#signup = Signup.new
end
def create
#signup = Signup.new(params[:id])
if #signup.save
else
render 'new'
end
end
end
The problem is most likely because of an instance variable that you're using in your form_for not being set to an ActiveRecord object. It appears that you are setting it correctly in your "new" action, but it's not clear that you're rendering the correct template since you mention the form being in "home.html.erb"?
Either way, ensure that whatever you're using in the form_for is set to a valid ActiveRecord object and your problem may be solved.
In addition, you may want to change your create action to use all the params from the form:
#signup = Signup.new(params[:signup])
Add this to the home action in the pages controller:
#signup = Signup.new
The reason for the error is that you are using #signup in your form, but you didn't define it in your show controller action.
I'm using the generic search form, and my url after the search looks like
http://localhost:3000/search?commit=Search&page=2&query=feature&utf8=%E2%9C%93
The search works fine, but I would like to remove the default "utf8=✓" and "commit=Search" parameters from the URL, I'm also using will_paginate and I would like the &page=2 to be after the query parameter leaving it like this:
http://localhost:3000/search?query=feature&page=2
My code:
#posts_controller.rb
def search
query = '%'+params[:query]+'%'
#posts = Post.find(:all, :conditions => ["content LIKE ? or title LIKE ?", query, query]).paginate(:page => params[:page], :per_page => 5)
end
and
#html form
<%= form_tag(search_path, :method => 'get') do %>
<%= text_field_tag "query" %>
<%= submit_tag "Search" %>
<% end %>
and
#routes.rb
match '/search', :to => 'posts#search'
Thanks.
See similar questions:
Rails 3 UTF-8 query string showing up in URL?
removing "utf8=✓" from rails 3 form submissions
Basically, to remove 'commit=Search' add :name => nil to the submit_tag. IE needs the utf8 character. However, the second link has a initializer method to remove that part.
In this video, Ryan Bates talks about the name: nil fix (without ajax): http://railscasts.com/episodes/37-simple-search-form
You cant just remove it from url as far as YOU send it.
To clean up will_paginate try this
<%= will_paginate #whatever, params => params.merge({:commit => nil, :utf8 => nil}) %>
I solved utf problem by using
<form action="<%= root_path %>" method="get" >
...
</form>
instead of form_tag, it solved it.
Ryan Bates did a nice screen cast on exactly what you're trying to do (plus some more).
http://railscasts.com/episodes/240-search-sort-paginate-with-ajax