Using this previous question as a guide, I've attempted to create a ul navigation header above a container that renders partials within the container when clicked. (Hopefully that makes some sense, but it may not be important.) Until the links for the partials are clicked, I have it rendering a partial by default.
However, when I went to click my link_to in hopes of rendering the partial I get the following error:
uninitialized constant ProfileController
I'm using Rails 3. Here's my relevant code:
ProfilesController:
def show_about
#is_on_show_about = true
end
def show_info
#is_on_show_info = true
end
views/profiles/show.html.erb:
<div id="info">
<div id="infoContainer">
<% if #is_on_show_about %>
<%= render :partial => 'show_about' %>
<% elsif #is_on_show_info %>
<%= render :partial => 'show_info' %>
<% end %>
<ul id="info">
<li>
<%= link_to 'About', show_about_path, :remote => true %>
</li>
</ul>
<ul id="settingsLinks">
<li>Advice</li>
<li>
<%= link_to 'Info', show_info_path, :remote => true %>
</li>
</ul>
</div>
<%= render :partial => 'show_about' %>
Routes.rb:
map.show_info 'profiles/:id/info', :controller => 'profile', :action => 'show_info'
map.show_about 'profiles/:id/about', :controller => 'profile', :action => 'show_about'
Can anyone help me fix this and explain what went wrong?
Both of your routes are incorrect.
If your controller is indeed named ProfilesController (plural) then your routes should use :controller => 'profiles', instead of :controller => 'profile'.
Related
I got such a problem - there is a partial and I can not pass a variable there:
in partial I have;
<%= object.title %>
How I pass variables:
<%= render :partial => 'shared/post_preview', :locals => { :object => article } %>
the error I see looks like
**undefined local variable or method `object'**
Any ideas? I tried already everything seems...
also tried:
<%= render :partial => 'shared/post_preview', :object => article %>
<%= render 'shared/post_preview', :object => article %>
<%= render :partial => 'shared/post_preview', :object => article %>
everytime i see the same error...
Use this:
Assuming you have defined #article instance variable in action.
<%= render 'shared/post_preview', object: #article %>
This must solve your problem.
the problem was in the commented code in the partial file. Somehow it was counted like an actual code...
<!--
<div class="row">
<div class="col-lg-6">
<%= render :partial => 'shared/post_preview' %>
<%= render :partial => 'shared/post_preview' %>
</div>
<div class="col-lg-6">
<%= render :partial => 'shared/post_preview' %>
<%= render :partial => 'shared/post_preview' %>
</div>
</div>
-->
I have view which contains a search form like below:
<div>
<%= form_tag :action => 'search' do %>
<p>
<%= text_field_tag :search, params[:search] %>
<%= submit_tag "Search", :class => 'nil' %>
</p>
<% end %>
</div>
whenever i am clicking search button it is calling a new view which i dont want.
I want to be in the same page to do some action and display the result there only.
Can any one direct me how to do this?
I found the solution from the following blog
http://www.alfajango.com/blog/rails-3-remote-links-and-forms/
if your search form is in index.html.erb and you want the search result on the same page then
in your controller
def index
if params[:search]
# search query
else
# listing records
end
end
in view
replace <%= form_tag :action => 'search', :method => :get do %>
with <%= form_tag :action => 'index' do %>
my routes
TerritoryManagement::Application.routes.draw do
get 'new' => 'territories#new', :as => 'new'
root :to => 'territories#index', :as => 'territories'
resources :territories
resources :users
end
create in my controller
def create
#territory = Territory.new(params[:territory])
if #territory.save
redirect_to root_url, :notice => "Product successfully created!"
else
render "new"
end
my view
<%= form_for(#territory) do |f| %>
<div class="field">
<%= f.label :name %><br />
<%= f.text_field :name %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
this generates
<form accept-charset="UTF-8" action="/" class="new_territory" id="new_territory" method="post">
I know that this action="/" is the problem, but I don't understand why it's being generated? How to modify my routes that the app will create the record and then goes to the index or edit view?
Thanks
Thomas
This worked for me:
TerritoryManagement::Application.routes.draw do
get 'new' => 'territories#new', :as => 'new'
resources :territories
root :to => 'territories#index'
end
It seems that the :as option was causing some issue. That's there to give the route a name, but since you've already done 'resources :territories' you already have named routes for the standard CRUD actions. I also moved the root route to the end of the file. I can't remember why, but it seems like this was a 'Best Practice' back in the Rails 2.3 days.
I have a partial that I use to show errors from an object onto a form.
<% if object.errors.any? %>
<div id="error_explanation">
<h2>Oops, looks like <%= pluralize(object.errors.count, "error") %>
occured:</h2>
<br />
<ul>
<% object.errors.each do |key, msg| %>
<li><%=key%><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
It works great for 1 model.
However I can't figure out how to make it work for a form that has two models.
Any ideas? I don't want to use the plugin I would like to have more control.
Just output the partial for each model in the form view with 2 models and pass actual model instances into this partial as local variable:
<%= render :partial => 'name_of_partial_to_show_model_errors', :locals => {:object => #model1} %>
<%= render :partial => 'name_of_partial_to_show_model_errors', :locals => {:object => #model2} %>
Given:
The following partial:
<%= form_for #user_session, :url => user_session_path do |f| %>
<%= f.error_messages %>
<span class="field-group">
<div>
<%= f.label :login, "Login Name:" %><br />
<%= f.text_field :login %><br />
</div>
</span>
<div class="clear"></div>
<span class="field-group">
<div>
<%= f.label :password, "Password:" %><br />
<%= f.password_field :password %> <span class="hint">Reminder: Your password is case sensitive.</span><br />
</div>
</span>
<div class="clear"></div>
<span class="field-group">
<div>
<%= f.check_box :remember_me %> <%= f.label :remember_me %>
</div>
</span>
<div class="clear"></div>
<%= f.submit "Login" %>
<% end %>
And the following route:
$ rake routes | grep user_session | grep show
user_session GET /user_session/:id(.:format) {:action=>"show", :controller=>"user_session"}
And the following route configuration:
# user session stuff
resources :user_session do
member do
put :forgot_password
put :terms
get :terms
end
end
match '/login', :to => 'user_sessions#new', :as => 'login'
match '/logout', :to => 'user_sessions#destroy', :as => 'logout'
Problem:
When I call the page that uses this partial, I get the following error:
ActionController::RoutingError in User_sessions#new
Showing /app/views/edit_shared/_login.html.erb where line #2 raised:
No route matches {:action=>"show", :controller=>"user_session"}
Extracted source (around line #2):
1:
2: <%= form_for #user_session, :url => user_session_path do |f| %>
3: <%= f.error_messages %>
4: <span class="field-group">
5: <div>
So that route exists, but rails says it doesn't. So what gives? It was fine until I started upgrading to Rails 3.0.5.
Try removing the :url => user_session_path from your form. By specifying form_for #user_session, Rails will look up the user_session RESTful routes and generate the URL for you.
changing :url => user_session_path to :url => {:action => :create} did the trick.
Your route only accepts GET requests. A form sends a POST request by default.
Edit:
First you should use your named route instead of user_session_path:
<%= form_for #user_session, :url => login_path do |f| %>
And the action you want is create, not new (which displays the form):
match '/login', :to => 'user_sessions#create', :as => 'login'