Rails 3: Path not responding in view but working in console - ruby-on-rails

I can run this command on my console:
#user.relationships.find_by_followed_id(9)
But I can't use this on my view:
<%= form_for current_user.relationships.find_by_followed_id(9), :html => { :method => :delete } do |f| %>
The error is:
undefined method `relationship_path' for #<#<Class:0x00000100bdc008>:0x00000100bc1f50>
I can't figure it out why.

you do not have a route for your relationship resource/model defined ...

Related

Using a partial from another view while that model's routes are nested in the current view Rails 3

I've got two models/views, User and PersonalInfo and I'm trying to call the _form.html.erb partial in the 'user#show' action, but I'm getting an error:
No route matches {:controller=>"personal_info"}
I suspect the issue is that my PersonalInfo routes are nested within the User routes and Rails isn't using the right one, but I don't know how to make it use the right one. Here's the line that's calling the form:
app/views/users/show.html.erb:
<%= render :partial => "/personal_info/form", :locals => {personal_info: #personal_info } %>
app/views/personal_info/_form.html.erb:
<%= form_for #personal_info, url: user_personal_info_index_path, html: { method: :post } do |f| %>
routes.rb:
resources :users do
resources :personal_info
end
Do I either have to declare something in the personal_info controller or specify the app to use the users/:user_id/personal_info route?
I was able to get this resolved by changing:
<%= form_for #personal_info, url: user_personal_info_index_path, html: { method: :post } do |f| %>
to:
<%= form_for #personal_info, url: new_user_personal_info_index_path, html: { method: :post } do |f| %>

No route matches [POST] "/author/test_author/24/steps_author/13"

I have a form that will work sometimes and sometimes not and I can't work out what is changing between times, other than restarting the rails app. Currently it isn't working.
I have these entries in my routes file:
constraints :subdomain => 'my' do
namespace 'my', path: nil do
namespace 'author' do
resources :test_author do
resources :steps_author
[...]
end
end
end
end
The particular routes I'm interested in here produce this from rake routes
my_author_test_author_steps_author GET /author/test_author/:test_author_id/steps_author/:id(.:format) my/author/steps_author#show {:subdomain=>"my"}
PUT /author/test_author/:test_author_id/steps_author/:id(.:format) my/author/steps_author#update {:subdomain=>"my"}
DELETE /author/test_author/:test_author_id/steps_author/:id(.:format) my/author/steps_author#destroy {:subdomain=>"my"}
My form opening looks like this (using simple form and bootstrap):
<%= simple_form_for #step, :url => my_author_test_author_steps_author_path(#step), :html => { :class => 'form-horizontal' } do |f| %>
Can anyone shed some light on what is happening?
Update
Based on the help from juanpastas it seems that the form is rendering correctly however Rails is interpreting the request as a POST and not a PUT. Though for the life of me I can't work out why.
You need to pass complete params in route
<%= simple_form_for #step,
:url => my_author_test_author_steps_author_path(author, #step),
:html => { :class => 'form-horizontal' } do |f| %>
After reading your title I see you should be making POST to /author/test_author/24/steps_author/ without the last number. What about:
<% url = !#step.persisted? ? '/author/test_author/24/steps_author/' : my_author_test_author_steps_author_path(author, #step) %>
<%= simple_form_for #step,
:url => url,
:html => { :class => 'form-horizontal' } do |f| %>
I have harcoded route because I am not sure about your route helper name. You can run rake routes to find that helper.

getting error while edit_order_path

In my index.html.erb I have
<%= link_to 'Edit', edit_order_path(order) %>
Whenever I try to edit the order contents i got the following error:
NoMethodError in Order#index
undefined method `edit_order_path' for #<ActionView::Base:0x13f4aeb>
How can I overcome it?
Add following line of code in your routes.rb resources :orders
or add following line of code in routes.rb
match 'orders/:id/edit' => 'orders#edit', :as => :edit_order
I assume, your controller name as orders and action edit.
use following code for form
<% form_for(#order, :url => {:action => :update}) do |f| %>

No route matches in Rails 3.0.4

Been staring at this problem for a while now. Here's the error I'm getting when I try to view the page.
No route matches {:action=>"confirm", :controller=>"locations"}
This is what I have in the view.
<%= form_for(#location, :url => { :action => :confirm }) do |f| %>
<% end %>
And I think my routes file is set up correctly.
Finder::Application.routes.draw do
resources :locations do
member do
post :confirm
end
end
root :to => 'locations/index'
end
Any ideas?
Updated:
Ran rake routes and get what I think is correct.
confirm_location POST /locations/:id/confirm(.:format) {:action=>"confirm", :controller=>"locations"}
You can debug your routes easily in the future by running $ rake routes and looking at the output. ;)
I think what is happening is that your post :confirm isn't registering the route you're expecting. In the guides, match and it's brethren accept a string as a URL segment like so:
resources :locations do
member do
post 'confirm'
end
end
Note that "confirm" is now a string instead of a symbol.
If this doesn't help, run $ rake routes and tack the output onto your question.
Update
After seeing your rake output, I think that you just need to specify the POST method on your form_for:
<%= form_for(#location, :url => { :action => :confirm }, :method => :post) do |f| %>
<% end %>
You can also make this more readable using that helper method that Rails defines:
<%= form_for(#location, :url => confirm_location_path(#location), :method => :post) do |f| %>
<% end %>
Did you define the confirm action in your LocationsController?
Try adding a :method => :post to your form_for
<%= form_for(#location, :url => { :action => :confirm }, :method => :post) do |f| %>
<% end %>
Make sure that form_for doesn't sneak in a hidden field with _method=put if you have declared the route as accepting only post in your routes file.

Ruby on Rails: How do I specify which method a form_tag points to?

I have this:
<% form_tag :controller => :proposals, :method => :bulk_action do %>
but it errors in the create method... which is weird because I'm trying to tell stuff to delete.
The :method parameter specifies the HTTP request method. You're probably looking for :action instead:
<% form_tag :controller => :proposals, :action => :bulk_action do %>
This will create a form that points to bulk_action in ProposalsController.

Resources