Form for getting routes to wrong controller - ruby-on-rails

I have this form:
<%= form_for #account, :as => :account, :url => current_account_path, :html => { :class => 'block' } do |f| %>
<%= f.text_field :name %>
<%= f.submit 'Update', :class => 'button' %>
<% end %>
which is being loaded from users#edit
However, I want it to submit to accounts#update. But, no matter what I do it always submits to users#update. Am I not specifying it correctly?

You're specifying the URL you want to submit to with the :url key in your hash: if that isn't the correct route, you should specify it there. So run rake routes, find the right URL in that list, and then put the correct path helper in there and it should work.

Related

Passing model id through rails route in form

I'm calling a custom action with simple_form. I'm having trouble passing the :id parameter to the action.
routes
post '/posts/:id/admin_vote' => 'posts#admin_vote', as: 'admin_vote'
form
<%= simple_form_for :post, url: admin_vote_path(:post_id), :html => {:class => 'form-inline admin-vote-form'} do |f| %>
<%= f.select :vote, 1..20 %>
<%= f.submit 'Vote', :class => 'btn btn-primary btn-xs' %>
<% end %>
partial render
<%= render 'layouts/admin_vote', :locals => { :post => post, :post_id => post.id } %>
For some reason the action receives params[:id] = 'post_id' instead of the actual id.
You're providing :post_id symbol to the admin_vote_path, so it uses that. Change it to:
admin_vote_path(params[:post_id])
or a different parameter depending on the context of your form.
it seems to me your form should be
<%= simple_form_for post, url: admin_vote_path, :html => {:class => 'form-inline admin-vote-form'} do |f| %>
<%= f.select :vote, 1..20 %>
<%= f.submit 'Vote', :class => 'btn btn-primary btn-xs' %>
<% end %>
you should build form based on variable post

Rails custom form action

I have a few forms on a page - I want each to be pushed to a custom controller action.
Each form is going to upload an image (an avatar, a background image, etc), and I want each form to use a different controller action - however, I'm getting 404 errors.
<%= form_for #shap, :remote => true, :url => {:action => 'upload_avatar'}, :html => {:id => 'edit_shop_2'} do |f| %>
<%= f.file_field :avatar, :class=>'avatar_upload' %>
<% end %>
I have the route properly defined, a controller action, and associated javascript file to return on success. Why the 404?
you also need to define controller name here
<%= form_for #shap, :url => {:controller=>"your controller", :action => 'upload_avatar'}, :remote => true, :html => {:id => 'edit_shop_2'} do |f| %>
<%= f.file_field :avatar, :class=>'avatar_upload' %>
<% end %>

Simple_form can't find path for instance

I get this in my form:
undefined method `urls_path' for #<#<Class:0x000000048ec778>:0x00000005583090>
This is my form:
<%= form_for #url, :html => {:class => 'form-horizontal' } do |f| %>
<%= f.input :url %>
<%= f.input :contact_name %>
<%= f.input :contact_email %>
In my controller action I do, #url = Url.new
I have another app, that does things in the way way, so I don't know what's wrong with it.
Try to check your routes are correct, run
rake routes
you should see
urls GET / urls#index

Trying to do simple_form_for on the following example code

I'm new to ruby-on-rails and trying to refactor the following form_tag into simple_form_for code and I was getting a weird error in log stating:
"customers_customers_customers_path' no methods found.
Any idea on which part of the code is wrong?
<%= form_tag projects_path, :method => 'get', :id => "ajax_search_form" do %>
...
<% end %>
I refactor it into:
<%= simple_form_for #projects, :html => { :id => 'ajax_search_form' }, :method => 'get' do |f| %>
...
<% end %>
What am I doing wrong?
simple_form_for is mainly used to associate the form to a model just like form_for does. In your code, you used form_tag to associate to a path, not a model. The right way to implement simple_form_for here is:
<%= simple_form_for :projects, :url => projects_path, :method => 'get', :id => "ajax_search_form" do $>
...
<% end %>

Rails3 display form_for form gives routing error if on other erb page

I have a'Remove' button in a show erb of the trackers_controller.show:
<%= form_for :user_tracker, :url => user_tracker_path,:method => :delete do |f| %>
<%= f.hidden_field :tracker_id, :value => #tracker.id %>
<%= f.submit :save, :value => 'Remove' %>
<% end %>
This works fine and calls user_trackers_controller.destroy
The User models looks like:
has_many :user_trackers
has_many :trackers, :through => :user_trackers
If I put the exact same button in another erb I get this error:
No route matches {:action=>"show", :controller=>"user_trackers"}
I have a few different combinations like :html => {:method => :delete }
If I try it this way
<% current_user.user_trackers.each do |user_tracker| %>
<%= user_tracker.tracker %>
<%= form_for user_tracker, :method => :delete do |f| %>
<%= f.submit :delete, :value => 'Remove' %>
<% end %>
I get the same routing error
Here are the routes:
user_trackers GET /user_trackers(.:format) user_trackers#index
POST /user_trackers(.:format) user_trackers#create
new_user_tracker GET /user_trackers/new(.:format) user_trackers#new
edit_user_tracker GET /user_trackers/:id/edit(.:format) user_trackers#edit
user_tracker GET /user_trackers/:id(.:format) user_trackers#show
PUT /user_trackers/:id(.:format) user_trackers#update
DELETE /user_trackers/:id(.:format) user_trackers#destroy
I do not understand, why will it not pick up that this is a destroy when in an unrelated erb?
Edit:
This is one seems to work but the html generates ids and classes like edit_user_tracker_7 but also the javascript to make it a delete so it seems I still have something wrong:
<%= form_for user_tracker, :url => user_tracker_path(user_tracker), :method => :delete do |f| %>
<%= f.hidden_field :tracker_id, :value => user_tracker.tracker_id %>
<%= f.submit :delete, :value => 'Remove from my portfolio' %>
<% end %>
in first line it should be
:url => user_tracker_path(#user)
Its not a bug, he just have to know who to remove :) so he needs id (in REST).
Also :method should be in :html
:html => { :method => :delete }
or
html: { method: "delete" }
with 1.9+ notation.
full form_for
user_tracker_path(#user), :html => {:method => :delete} do |f| %>
ofc if in your case #user is current_user then you have to swap it :)
Sorry for typos & english i'm not native :)
Cheers!

Resources