Rails 3 Route: button_to HTML INPUT Action not working - ruby-on-rails-3

I have a strange problem that I think has to do with routes.
In my "view" I have this:
<%= button_to "New Item", new_proposal_pitem_path(#proposal), :method => :get %>
I want to click the "New Item" button, and create a new pitem for a proposal. This generates the HTML I would expect:
<form method="get" action="/proposals/1234/pitems/new" ...><input ...></form>
However, what really happens is, when I click on the button it attempts a GET on /proposals/1234 rather than /proposals/1234/pitems/new. This gives me a "show" page rather than a "new" page. Interestingly, I can manually put the {site}/proposals/1234/pitems/new directly into the web browser HTTP address and get what I want (the "new" page). But rails is, on its own, translating it first to /proposals/1234 if I leave it to its own in response to the button click.
To make this more mysterious, I have a similar item on the same form which looks exactly the same way:
<%= button_to "New Payment", new_proposal_payment_path(#proposal), :method => :get %>
which generates the same HTML as the other case:
<form method="get" action="/proposals/1234/payments/new" ...><input ...></form>
But this one works! It takes me right to /proposals/1234/payments/new when I click the button, just like I'd expect. I just don't understand what makes these behave differently.
My full routes file looks like this:
TCoB::Application.routes.draw do
resources :proposals do
resources :pitems, :payments
get 'list', :on => :collection
end
resources :pitems do
get 'list', :on => :collection
end
resources :invoices do
resources :iitems, :payments
get 'list', :on => :collection
end
resources :iitems do
get 'list', :on => :collection
end
resources :payments do
get 'list', :on => :collection
end
resources :ids
resources :clients do
resources :proposals, :invoices
# Route GET /cients/list
get 'list', :on => :collection
get 'list_proposals', :on => :collection
get 'list_invoices', :on => :collection
end
get "home/index"
root :to => "home#index"
end
Can someone shed light on this issue?
Thanks!

Small helper method that uses JavaScript:
def button_link_to(name, url)
"<button type=\"button\" onclick=\"window.location.href='#{url}';\">#{h(name)}</button>".html_safe
end
button_link_to "New Item", new_proposal_pitem_path(#proposal)

Related

how to pass id to controller in rails 3?

I have used link_to to call controller action.
<%= link_to "Inactive", {:controller => :leave_details, :action => :deactivate}, {:method => :post } %>.
but my requirement is to pass id also to controller action but when i try to pass id(:id=>leave_detail.id) it is not showing in params. my params look like
{"_method"=>"post", "authenticity_token"=>"3vfyGQ5V6GQXRt2aQt+DOT0b3eGgP7B401uclnIGLUU=", "action"=>"deactivate", "controller"=>"leave_details"}.
In route file i have added
resources :leave_details do
post :deactivate, :on => :collection
end
can anyone tell me what is going wrong in this code or what i am missing.
thnks

rails 3 absolute link to generated url

I am looking into generating urls which look like https://mywebsite.com/user/check_email_confirmation/46cee41bc2044104aff8cf7746687ea8
for the famous "please click this link to confirm your email".
So I have a route which I intend to use for this in my routes.rb:
resources :user do
collection do
get 'check_email_confirmation/:generated_url', :to => 'user#check_email_confirmation'
end
end
Now, my trouble is to generate the full address above. I cah generate the SHA (46cee41bc2044104aff8cf7746687ea8) part, but how do I write the "https://mywebsite.com/user/check_email_confirmation/" part in my view?
Added view's code:
<p>Please click <%= url_for(:action => 'check_email_confirmation', :controller => 'user', :only_path => false, :id => #confirmation_url) %> to confirm your e-mail address.</p>
Replace id: with generated_url:.
You could also simplify the url_foring with naming the route by as: in routes
get 'check_email_confirmation/:generated_url', to: 'user#check_email_confirmation', as: :confirmation
More on this at http://viget.com/extend/rails-named-routes-path-vs-url
get "check_email_confirmation/:id" => "users#check_email_confirmation", :as => "check_email_confirmation"

Links to resources with friendly URLs

I'm trying to write a blog in Rails 3, so I have posts. I want to make nice routes for the posts like this: posts/year/month/day/post-title. So I overrided to_param in model/post.rb and use friendly_id for title:
extend FriendlyId
friendly_id :title, :use => :slugged
def to_param
"#{year}/#{month}/#{day}/#{title.parameterize}"
end
And I added this to routes.rb:
resources :posts do
collection do
match ":year/:month/:day/:id", :action => "show"
end
member do
post 'comment'
delete 'comment/:comment_id', :action => 'destroy_comment', :as => 'destroy_comment'
end
end
In show this works:
<%= link_to image_tag("/images/edit_icon.png"),
{ :controller => 'posts', :action => 'edit' }, :id => #post %>
But in index it says
routing error:
No route matches {:action=>"edit", :controller=>"posts"}.
I'm new to Rails and I haven't managed to find out what causes this error. Can anyone help me figure out what I do wrong?

Share resources between layouts in Rails 3

OK, so Ive set up my mailer in Rails which works fine, but I wanted to make a new action (or maybe just a view?) to have a slimmed down contact form in a lightbox. I can do that all fine and dandy but it would use the default layout which I dont want. So I added:
render :layout => 'lightbox'
to the action so that I could use a new layout. Unfortunately that seems to block off my access to the model as I get this error when the lightbox pops up
undefined method `model_name' for NilClass:Class
#on this line
<% form_for #contact, :url => {:action => "create"}, :html => {:method => :post} do |f| %>
So by using a different layout I cant use the resources I set up in my routes which is here:
resources :contacts, :only => [:new, :create], :as => :contacts
#Im passing in a name to the email form
match "contacts/direct/:name" => "contacts#direct", :as => :direct_email
I hope that made sense. But what do I do?

Rails 3 - link for delete item

I have list of images and for every image I have link for deleting. That link looks this:
<%= link_to 'delete image', {:controller => 'shops', :action => 'delimg', :imgid => u.id}, :confirm => 'Really?' %>
def logo
puts params[:imgid]
...
end
And I am getting an error Couldn't find Shop with ID=logo and app/controllers/shops_controller.rb:17:in `show' - I tried to add puts 'IN SHOW and it really looks, that after click on that link is called method show. I have no idea, how it is possible...
Could someone help me, please, where is the problem?
This is probably how I would have done it:
#routes.rb
resources :shops do
delete :delimg, :on => :member
end
By adding that, there will be a defined route to the delimg action mapped to the delete method. And that makes it possible to do the following in the view:
<%= link_to 'delete image', delimg_shop_path(u.id), :method => :delete %>
delimg_shop_path is a path helper that exists because of what was added in the routes.rb
you are displaying params[:ingid] in the logo method but in the link to action u have specified delimg??
modiefy your link to as
<%= link_to 'delete image', {:controller => 'shops', :action => 'logo', :imgid => u.id}, :confirm => 'Really?' %>
then it will work