I am very new to ROR and I love it so far as I develop my first app. I have a question related to my application template as I apply formatting to the nav menu.
Is it possible to check if a url path matches the root:to path set in config.rb? I have a helper method that returns the string "current" which adds the css class style to highlight the selected menu item. The helper method works fine as long as I'm not at the homepage. When I my url is www.localhost:3000/ the css current class is not applied to the Products link since the request_uri = "/" which doesn't equal "/products". I would like the css class "current" applied to the Products menu item when I'm on the homepage.
Is there any conditional logic I can use to get the root:to path and check if it matches the is_current's parameter path?
Here's my code:
routes.rb root:to setto point to the products index view
root :to => 'products#index'
application.html.erb
<%= link_to 'Products', products_path, :class => is_current(products_path) %>
<%= link_to 'Reports', reports_path , :class => is_current(reports_path) %>
application_helper.rb
def is_current(path)
if request.request_uri == path
return 'current'
end
end
Any help would be greatly appreciated.
Thanks,
bkasen
Would this work for you?
if current_page? root_path
for more info: http://api.rubyonrails.org/classes/ActionView/Helpers/UrlHelper.html#method-i-current_page%3F
If I read correctly, you want "onlink" css applied? so that when the user is on the home page, the home icon is a different color or something. If so then apply this in your header partial or where ever:
<li><%= link_to_unless_current "Home", root_path, :class => "navLinks" do link_to "Home", root_path, :class => "navLinks", :id => "onlink" end %></li>
I know this is an old question, but it may prove useful to someone else :)
Related
I have several static pages (About, Contact, Help) that are mapped in the routes.rb file like this:
get 'about', to: 'static#about', as: 'about'
get 'contact', to: 'static#contact', as: 'contact'
get 'help', to: 'static#help', as: 'help'
They are accessed in the layout partial, _footer.html.erb from this code:
<%= link_to "About", 'about_path', :class => '' %>
<%= link_to "Contact", 'contact_path', :class => '' %>
<%= link_to "Help", 'help_path', :class => '' %>
Everything works fine until I click on the footer links while I'm in a nested route like /users/current/edit (where I might edit my user-profile). For example, when I click on the ABOUT link at the bottom of the page, I would expect to be taken directly to the static#about route at about_path.
However, I am getting an ActionController exception (in development) and a page not found in production. It's trying to map to /users/current/about_path.
Any ideas on how to fix this?
See Thorin's answer. Remove the quotes around the path method call.
I have a property search page (:controller => 'properties', :action => 'index') that consists of a right sidebar that has a search form which displays the search results below the form. When the user clicks on a property in the right sidebar I want to display the details of that property in the main area of the index page on the left.
Right sidebar is a partial called properties/_property.html.erb:
<%= form_tag properties_path, :method => 'get' do %>
<%= text_field_tag 'location', (params[:location]) %>
<%= select_tag(:max, options_for_select([['No Max', ""], ['$100,000', 100000], ['$200,000', 200000], etc %>
more search fields for baths beds etc
<%= submit_tag "Search", :name => nil %>
<% end %>
<% #properties.each do |property| %>
<%= link_to([property.Address,property.City].join(", "), {:action => 'show', :id => property.id}) %>
<li><strong><%= number_to_currency(property.Price, :precision => 0) %></strong></li>
etc etc
<% end %>
The only way I know how to show the property details is with the 'show' action, but that takes the user to a new page, for example localhost:3000/properties/1865. I've made the 'show' view with the same layout as the 'index' view and have made the right sidebar a partial (properties/_property.html.erb) which appears on both 'show' and 'index' so when the user clicks on a property in the right sidebar and goes to localhost:3000/properties/1865 the property details are displayed correctly in the main area and the right sidebar is on the right.
But because localhost:3000/properties/1865 is a different page than localhost:3000/properties/index the search form in the right sidebar has forgotten it's parameters which means the list of search results in the right sidebar has changed back to the default list of all properties.
How can I display the 'show' action within a partial on the index page so the user's search parameters are remembered by the form in the right sidebar? Or if I have to go to the 'show' page how can I make the right sidebar stay exactly as it is?
Any ideas greatly appreciated, just a suggestion in the right direction would be good, have spent all day trying to figure it out and have got nowhere, thanks
I have made the show view with the same layout as the index view and
have made the right sidebar a partial (properties/_property.html.erb)
which appears on both show and index.
Yes but this will only get you a similar layout for both the pages. You want the list to persist between different requests.
You basically have two options. use session to remember the list which I wont recommend.
Other is you use form :remote => true or ajax and update the page partially.
EDIT:
What version of rails you are using? Do u have jquery loaded in your application?
Follow this SO POST.
You might have to change your show action a bit.
respond_to do |format|
format.js {render :partial => 'property_details' ,:layout => false}
format.html
end
Create a partial for property details and just put body content here.
And link will look like
<%= link_to "link name", {:action => :show, :id => item_id}, :remote => true ,:html => {:class => 'links_product'} %>
Also to update the view you may use(make sure you have rails.js in your page):
$(document).ready(
function(){
$("a.links_product").bind("ajax:success",
function(evt, data, status, xhr){
$("#response").html(data); // in case data is html. (_*.html.erb)
}).bind("ajax:error", function(evt, xhr, status, error){
console.log('server error' + error );
});
});
Have a div with id response or any valid id. Done!
Inside of a rails app that I am working, I modified the link_to helper slightly:
def link_to(*args, &block)
args[1] = params[:client_id].present? ? "#{args[1]}?client_id=#{params[:client_id]}" : args[1]
super
end
I did this so I wouldn't have to add the :client_id => params[:client_id] every time I wrote a link_to inside of the app. Well, I have kind of pigeon holed myself with the following problem...
If I have this link_to:
<%= link_to "Continue to billing info", add_product_path(:product_id => #product.id), :class => 'btn' %>
Using my link_to helper creates a link, like so:
http://localhost:3001/orders/add_product?product_id=35?client_id=HT274848772
I am at a slight loss on how to modify my helper so that the link will work as normal while including the :client_id param...
You want to add your parameter to the link url, not to the link itself. Maybe you should rewrite the url_for helper, which is the helper used by all of the url helpers ( http://apidock.com/rails/ActionView/Helpers/UrlHelper/url_for )
I am trying to generate a url in an actionmailer template. An example if the url I want to generate is
http://0.0.0.0:3000/users/confirm/lNbQxzFukYtEEw2RMCA
Where the last segment is a hash to identify the user
However when I use this
<%= url_for(:controller => 'users', :action => 'confirm', :id => #user.confirmhash, :only_path => false) %>
It generates this
http://0.0.0.0:3000/assets?action=confirm&controller=users&id=ZOR3dNMls8533T8hJUfCJw
How can I get it to correctly format? I have no idea where 'assets' is coming from.
Is there an easier way to use named routes that I am missing?
I've found the answer. As I'm still learning I've missed the option to create a named route. So this this the path I've taken.
In config/routes.rb
match 'user/confirm/:id' => 'users#confirm', :as => :confirm_account
Then in my action mailer template I've used
<%= link_to "Confirm your account", confirm_account_url(#user.confirmhash) %>
Which passes the :id into the controller action.
I'm trying to implement an 'edit' link that brings up a form to change a displayed attribute on a page.
My layout has:
<div id="company_info">
<%= yield :company_info %>
</div>
<div id="edit_company_info">
</div>
My view has:
<%= content_for :company_info do %>
<%= render 'company_info' %>
<%= link_to "Edit", 'company_info_form', :class => 'btn btn-mini', :method => :get, :remote => true %>
My controller has:
def company_info_form
#company = Company.get(params[:id])
respond_to do |format|
format.js
end
end
My company_info_form.js.erb file has:
$('#edit_company_info').html("<%= escape_javascript(render "company_info_form") %>");
Upon clicking the link, my server shows:
Started GET "/companies/company_info_form" for 127.0.0.1 at 2012-03-12 20:19:13 -0700
Processing by CompaniesController#show as JS
Parameters: {"id"=>"company_info_form"}
Completed 500 Internal Server Error in 1ms
RuntimeError (Called id for nil, which would mistakenly be 4 -- if you really wanted the id of nil, use object_id):
app/controllers/companies_controller.rb:9:in `show'
So I think this is a routing issue-- but I have no idea how to fix it. How do I get the company_id param that is on my current page to be recognized by the partial I'm loading as well?
I'm on /companies/1, but the link is to companies/company_info_form, losing the "company_id = 1" param.
Yes, the issue is with your routes and with your link as you have pointed out.
The first issue can be ascertained as it says Processing by CompaniesController#show as JS. So, its actually going to companies#show where it tries to find a company based on id. But, since no correct id is passed, it errors out.
The second issue is because your link is to companies/company_info_form, as you pointed out, since you have used 'company_info_form' as the path in your link for edit. And you haven't passed current company to the link either.
Since you haven't posted your routes file, which you should have, since you have identified a potential problem with routes , I'll present my own.
In your routes :
resources :companies do
member do
get 'company_info_form'
end
end
That will provide you with
company_info_form_company GET /companies/:id/company_info_form(.:format) companies#company_info_form
Then you can provide the link as :
<%= link_to "Edit", company_info_form_company_path(#company) %>