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!
Related
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) %>
In my Rails 3 app I have a link_to that takes the User from their profile page to the settings page. The settings page contains a form :partial for editing a profile. What I'd like to do is have the link_to in the Profile take the User to an anchor in the form partial. I've given it a go but am having trouble.
As my link_to I have:
<%= link_to('Visit the info section', settings_path, {:anchor => 'info'}) %>
Within settings_path is <%= render :partial => 'profiles/edit_settings_form' %>
Within that partial is:
<div class="infoBlock" anchor="#info">
</div>
I have a route for settings that I thought was causing the error:
match "/settings" => "settings#show", :as => 'settings'
I tried adding another match for the anchor to "profiles#edit_settings_form" but it didn't work. Any ideas?
<div class="infoBlock" anchor="#info">
is not proper way to do an anchor to a page. Correct syntax is something similar:
<a name="anchor"></a>
Now when you got to page with pagename#anchor, it redirects you to that block. For example:
<%= link_to "Block in home", :action => "home", :anchor => "anchor" %>
I have a model named "Post". I want to use a modal form to create a new post while at the SHOW view of another post. Meaning while I am viewing the post named "John" in its show view, I would like to be able to create a new post from right there.
The problem I have is that the ID of the new post remains the same as the post I am viewing, and causes the update action to be fired instead of the create action. Any suggestions on how to handle this?
Build a new post with Post.new and use that in a form_for:
<%= form_for Post.new %>
<%= render "form" %>
<% end %>
Of course this means you'll need to remove the form_for from your form partial if you have it in there, but that's a small sacrifice to make.
However if you really don't want to do that then you will have to pass through a local variable to the form partial to indicate which post you want to display. On the show page you'd have this:
<%= render :partial => "form", :locals => { :post => Post.new } %>
In the new and edit views you'd do this:
<%= render :partial => "form", :locals => { :post => #post } %>
The line is a little bit longer, but that would allow you to keep the form_for tag inside the form partial and not clog up the three other views with it.
This is my code
<%= link_to_function ("Add another url", :id=> "add_another_url_link_id") do |page|
page.insert_html :bottom ,:add_another_url, :partial =>'add_another_url', :locals => {:object =>Url.new, :url => section}
end%>
Here i am showing add_another_url partial under the bottom of add_another_ur div.
I have a list. At the end of row i am showing this link. When i click the link that will show form under the row. But when i click the link that is showing the form under the first row only.
I want to show the form for corresponding row. (I don't know how to use 'this' in this place)
Can Anyone help me.
I assume you generate this link for every row in your list. This is a bad idea because it will generate the same element id (add_another_url_link_id) for every row which is not valid html.
You should either generate individual ids:
<%- #items.each do |item| %>
<%= link_to_function "Add another url", :id => "add_url_for_item_#{item.id}" do |page| %>
...
And use the specific id to find the relevant row.
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 :)