Rails link_to anchor within partial - ruby-on-rails-3

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" %>

Related

Rails: No route matches [PUT] "/blog/2"

I am creating blog application in rails. I have a common form for creating and updating blog.
This is view of edit and new.html.erb
<%= render :partial => "form"%>
This is view of _form.html.erb blog:
<%= form_for #blog do |f| %>
<%= f.text_field :title, :placeholder => "Title" %><br>
<%= f.cktext_area :article, :placeholder => "Content", :ckeditor => {:toolbar => "MyToolbar"} %>
<%= f.submit %>
<% end %>
My blog is creating successfully but I am getting error on update action. This is my edit and update action in blog controller:
def edit
#blog = Blog.find_by_slug(params[:id])
end
def update
#blog = Blog.find(params[:id]) || not_found
#blog.update_attributes(params[:blog])
redirect_to "/blogs/#{#blog.slug}"
end
When I open form from edit view, and click on update button, it throws error:
No route matches [PUT] "/blog/2"
My routes.rb is:
resources :blogs
get 'blog', to: 'blogs#index'
get '/blog/:id', to: 'blogs#show', as: 'blog'
I am not getting where it is going wrong. I tried to add "url: blogs_path" in form_for, it removes the error but doesn't save the edit changes.
Can anybody help me where I am going wrong here?
Thank you.
Okay. I dont understand why you want to go against conventions. Anyway, using form_for resource would automatically generate action URL as a PUT to /resources/:id if its an update operation.
So to override this you need to do two things.
update your routes to support this:
Add this line to your routes file:
put 'blog/:id' => 'blogs#update', :as => 'update_blog'
It is important that you put this line above your 'resources :blogs` call.
2 . specify the URL to which the form should submit:
You will need to create the form tag like this:
<%= form_for #blog, :url => update_blog_path(#blog) do |f| %>
Try this and let us know.

Display 'show' action in partial within the 'index' action's view - rails

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!

No route matches [POST] "/contacts/1"

app/view/contact/show.html/erb
<%= form_for(#contact) do |f| %>
<p id="notice"><%= notice %></p>
<p>
<b>Firstname:</b>
<%= #contact.firstname %>
</p>
<p>
<b>Lastname:</b>
<%= #contact.lastname %>
</p>
<p>
<b>Email:</b>
<%= #contact.email %>
</p>
<p>
<b>Mobilephone:</b>
<%= #contact.mobilephone %>
</p>
<% end %>
<%= link_to 'Edit', edit_contact_path(#contact) %> |
<%= link_to 'List', contacts_path %>
in my view/contact/index.html.erb i have a button
<%= button_to 'show', contact %>
in my contacts_controller.rb i just use automatic setting like:
def show
#contact = Contact.find(params[:id])
respond_to do |format|
format.html # show.html.erb
format.json { render json: #contact }
end
end
in my routes file
match '/contacts/:id/edit', :controller => 'contacts', :action => 'edit'
match '/contacts/contact_:id/show', :controller => 'contacts', :action => 'show'
resources :contacts
resources :connections
resources :addresses
root :to => 'contacts#index'
and after running rake routes i got
/contacts/:id/edit(.:format) contacts#edit
/contacts/:id/show(.:format) contacts#show
contacts GET /contacts(.:format) contacts#index
POST /contacts(.:format) contacts#create
new_contact GET /contacts/new(.:format) contacts#new
edit_contact GET /contacts/:id/edit(.:format) contacts#edit
contact GET /contacts/:id(.:format) contacts#show
PUT /contacts/:id(.:format) contacts#update
DELETE /contacts/:id(.:format) contacts#destroy
connections GET /connections(.:format) connections#index
POST /connections(.:format) connections#create
new_connection GET /connections/new(.:format) connections#new
edit_connection GET /connections/:id/edit(.:format) connections#edit
connection GET /connections/:id(.:format) connections#show
PUT /connections/:id(.:format) connections#update
DELETE /connections/:id(.:format) connections#destroy
addresses GET /addresses(.:format) addresses#index
POST /addresses(.:format) addresses#create
new_address GET /addresses/new(.:format) addresses#new
edit_address GET /addresses/:id/edit(.:format) addresses#edit
address GET /addresses/:id(.:format) addresses#show
PUT /addresses/:id(.:format) addresses#update
DELETE /addresses/:id(.:format) addresses#destroy
root / contacts#index
And when i click the button 'show' i got Routes Error No route matches [POST] "/contacts/1" Could somebody help me checking what mistake i have maken,please? Thank you very much for helping.
Maybe it's easier if you use the standard routing offered by resources contacts - then you could simply use <%= link_to 'show', contact_path(contact) %>
Also, the button should perform a GET request, not a POST since that route is not defined.
If I were you I would remove the first two custom routes you declared and just rely on the resources :contacts method instead. This in combination with a GET request should fix your problem.

Ruby on Rails: Custom Actions (Follow up)

This question is a follow up to this previous question: Ruby on Rails: Custom actions
As a follow up, what would be the syntax to use a custom action in a form_for? For my app, I have a partial called _invite_form.html.erb, and set the form to have a :url specification that I thought would link the form to the invite action on the Users controller:
<div id = "invite_form">
<h1>Invite</h1>
<%= form_for(invited, :url => invite_user_path) do |f| %>
<div class="field">
<%= f.text_field :email, :class => "inputform round", :placeholder => "email" %>
</div>
<div class="actions">
<%= f.submit "Invite", :class => "submit_button round" %>
</div>
<% end %>
</div>
This partial is called on certain pages, and this error is given:
"No route matches {:action=>"invite", :controller=>"users"}"
In my routes.rb file I have included the appropriate lines:
resources :users do
member do
get :invite
post :invite
end
end
Why is it that the route doesn't work? How do I change these files to make the form use the action "Invite" on the Users controller?
** Forgot to mention earlier: I defined invited in the Users helper: users_helper.rb:
module UsersHelper
def invited
#invited = User.new(params[:user])
end
end
As you don't have a persistent User just yet, make this a collection operation by:
Changing invite_user_path to invite_users_path in your controller
Changing member do to collection do in your routes
invite_user_path expects a user as an argument. Try invite_user_path(invited). You will also need to save the user before you can compute a path to it.
To experiment, go into rails console and see the difference between the following:
app.invite_user_path
app.invite_user_path(User.first)
app.invite_user_path(User.new)

Rails3 nested layout and partials passing parameters

Hey,
I'm working on a page having a nested layout. first I have the applicationlayout with my "mainmenu" now I want to add a second menu only on this page. I got this working via
<% render :partial => "mypartial", :layout => 'navigation' %>
this adds my second navigation to the form and renders a partial.
At this point I try to distinguish between two different partials. so my file looks like this
<% if :passed_text == "page1" %>
<%= render :partial => "mypartial1", :layout => 'navigation' %>
<% else %>
<%= render :partial => "mypartial2", :layout => 'navigation' %>
<% end %>
my navigation is as follows:
<%= link_to "Mypartial1", partial_path, :passed_text => :page1 %>
<%= link_to "Mypartial2", partial_path, :passed_text => :page2 %>
<%= yield %>
but it ignores my parameters. I guess I'm missing something basic, but all this is new to me.
thanks for your help
okay I found an answer:
first I have to check for:
params[:passed_text]
instead of :passed_text
secondly passing the parameters has to be in brackets
partial_path( :passed_text => :page1)
this works fine