Rails not routing edit method using /edit - ruby-on-rails

I am using the following code to attempt to link the customer's name to the edit method.
<%= link_to "#{customer.name}", customer, method: :edit %>
But when I click on the name it opens up /customer/1 instead of /customer/1/edit
If I manually enter /customer/1/edit it opens the page correctly. The worst part is that it used to do it correctly and then stopped.
How do I get my edit method to use the /edit again?

Link will be like this..
<%= link_to "#{customer.name}", edit_customer_path(customer) %>
if you want to mention method in link_to, you can do it as,
<%= link_to "#{customer.name}", :controller => 'customers', :action => 'edit' %>

Related

Rails no action "edit" in forum_posts_controller

The forum_posts controller is located in app/controllers/forum_threads/forum_posts_controller.rb
I don't know if I have to call forum_threads:forum_posts in the link_to.
Controller:
http://pastebin.com/t9vuyxdP
HTML:
http://pastebin.com/LextuZ74
On a side note, how do you add a button to the link_to? I've tried adding :class => "button" at the end, doesn't cause an error but still just shows a link not a button.
If you want to use link_to with older-style controller/action/id arguments:
<%= link_to "Edit", :controller => "forum_threads/forum_posts", :action => "edit", :id => forum_post.id, :forum_thread_id => #forum_thread.id %>
But Rails prefer newer RESTful routes whenever possible. If you define your controller correctly in routes.rb, you can write link_to like this:
<%= link_to "Edit", edit_forum_thread_forum_post_path(#forum_thread, forum_post) %>
UPDATE 18/11/2015: You forgot to include forum_thread_id in link_to as I saw your route require forum_thread_id and id param to make it work.
For more information on link_to, refer to: http://apidock.com/rails/ActionView/Helpers/UrlHelper/link_to

My link_to routes changing?

I'm currently using devise with the following link_to url to "submit" a Resource (resources_controller, resource.rb model)
This is in the menu:
<li><%= link_to "Submit Resource", :action => 'new', :controller => 'resources' %></li>
It works fine if I'm not on a devise login page (user signup, user login, etc)
otherwise it changes the url from /resources/new
to this:
http://localhost:3000/assets?action=new&controller=devise%2Fresources
The only reason i linked it using the first piece of code above is because I'm not sure if theres a better way to link to a particular REST action directly for a given controller (I'm not using :index)
Use <%= link_to "Submit Resource", new_resource_path %>. Before use it you should make sure if you have line resources :resources line in your routes.rb

Interpolated Ruby within a method call?

On my user model, I have a bunch of attributes like is_foos_admin and is_bars_admin that determine which kinds of records a user is allowed to edit.
I'd like to DRY out my edit links, which currently look like this:
<%= link_to 'Edit', edit_foo_path(foo), :class => 'edit' if current_user.is_foos_admin? %>
...
<%= link_to 'Edit', edit_bar_path(bar), :class => 'edit' if current_user.is_bars_admin? %>
I want to make a helper that lets me pass in a foo or bar and get back a link to edit it, like so:
<%= edit_link_for(foo) %>
The helper might look like this (which doesn't work):
def edit_link_for(thing)
if current_user.is_things_admin?
link_to 'Edit', edit_polymorphic_path(thing), :class => 'edit'
end
end
The model-agnostic edit_polymorphic_path method gets me halfway there, but it's the "is_things_admin" method that I don't know how to universalize. If I could use interpolated Ruby inside of a helper, I'd want to do something like
if current_user.is_#{thing.class.name.downcase.pluralize}_admin?
But of course that doesn't work. Any ideas?
Try using send:
if current_user.send("is_#{#model}_admin?")

Rails button_to fails with path doesn't exist for a path that exists

Writing my first, very simple Rails application, a simple admin app to track work for one of our departments. The generated index page for people has a link_to on it to add a new person. I tried to change that to button_to and it fails saying the path /people/new doesn't exist, though obviously it does since link_to goes to the same place.
I'm using Rails 3/Ruby 1.9.2. I have this code on my /app/views/people/index.html.erb page:
<%= link_to 'New Person', new_person_path %>
<%= button_to "New", :controller => "people", :action => "new" %>
The link_to works. The button_to fails with this:
Routing Error
No route matches "/people/new"
Also tried just
<%= button_to 'New Person', new_person_path %>
Same error. Odd.
button_to defaults to the post method. Try putting :method => :get in there. This is why link_to works.
There's a good explanation for this, as always :)
link_to uses GET as default, where button_to uses POST. And there's no POST route that matches, only a GET route.
If you want to use button_to, you can add :method => :get to your buttons params and it will use GET.
Did you set up your routing options in config/routes.rb? Check if you have this in your routes.rb file:
resources :people
Check this guide for more informations about how routes work.
Is your button_to inside a form? button_to creates a form of its own so this would create a form within a form and likely break routing.

Routing problem with calling a new method without an ID

I'm trying to put together a form_tag that edits several Shift objects. I have the form built properly, and it's passing on the correct parameters. I have verified that the parameters work with updating the objects correctly in the console. However, when I click the submit button, I get the error:
ActiveRecord::RecordNotFound in ShiftsController#update_individual
Couldn't find Shift without an ID
My route for the controller it is calling looks like this looks like this:
map.resources :shifts, :collection => { :update_individual => :put }
The method in ShiftsController is this:
def update_individual
Shift.update(params[:shifts].keys, params[:shifts].values)
flash[:notice] = "Schedule saved"
end
The relevant form parts are these:
<% form_tag( update_individual_shifts_path ) do %>
... (fields for...)
<%= submit_tag "Save" %>
<% end %>
Why is this not working? If I browse to the url: "http://localhost:3000/shifts/update_individual/5" (or any number that corresponds to an existing shift), I get the proper error about having no parameters set, but when I pass parameters without an ID of some sort, it errors out.
How do I make it stop looking for an ID at the end of the URL?
I think that you need to tell the form tag helper you want to use PUT instead of POST
<% form_tag( update_individual_shifts_path, :method => :put) do %>
... fields ....
<%= submit_tag "Save" %>
<% end %>
Amazingly, it turns out that I was able to fix this by a combination of renaming the method and passing a dummy variable. Changes were to the lines:
form.html.erb:
<% form_tag( poop_individual_shifts_path ) do %>
routes.rb:
map.poop_individual_shifts "poop_shifts", :controller => 'shifts', :action => "poop_individual", :method => "put", :id => 4
map.resources :shifts
There I pass it an ID of 4 every time, it doesn't matter, it's not actually doing anything with the shift object it goes and grabs, it's just ... I don't know, a hack, I guess.
shifts_controller.rb:
def poop_individual

Resources