Field accessing incorrect controller action on onChange - ruby-on-rails

I have a website and before i included the design and some changes(layout, scripts, etc), it was working fine. Then, I don't know what happened but the onChange method from my select field is accessing "show" action instead of the one that's written in the line.
I have the following at my New view:
<%= collection_select('category', 'id', #categories, 'id', 'name', {:prompt => "Choose a category"}, {:onchange => "#{remote_function(:url => {:controller => 'announcements', :action => "update_subcategories"}, :with => "'parent_id='+value")}",:class => 'newAd_box2_inputField'}) %>
The idea is that it updates a second select field with records related to the selected one.
The second select looks like this:
<%= render :partial => 'category_select', :object => #subcategories %>
Again, it was working great before I introduced some changes, but now it just won't go to "update_subcategories" action, it just goes to "show".
In my routes.rb I've got the following:
map.show "/announcements/:permalink", :controller => 'announcements', :action => 'show'
map.new "/announcements/new", :controller => 'announcements', :action => 'new'
Does anybody know what's going on?

This is your problem.:
map.show "/announcements/:permalink", :controller => 'announcements',
:action => 'show'
Rails will use the first route to appear in routes.rb that matches the parameters given. This behaviour kicks in both when generating urls for output with url_for (which is what the :url option for remote_function does in the background) and mapping urls received by the server to actions.
The above route will generate the route /announcments/update_subcategories so the link will appear right when you view source the source in your browser. But when you go to that link, Rails will match it to the show action of the announcements controller.
The fix depends on which method you're using to define routes for the announcements controller. Regardless of which fix you use, it must come before the bad route. (map.show "/announcements/:permalink", :controller => 'announcements', :action => 'show')
If you're using restful routes the fix is to add a :collection option to the definition.
map.resources :announcements, :collection => {:update_subcategories => :get}
If you're not using restful routes the fix is to add a route.
map.connect "/announcements/update_subcategories",
:controller => "announcements", :action => "update_subcategories"

Related

Rails : not routing properly, redirect back to another controller

I have a page on the website, which has lots of anchor link eg #menu | #sauces etc
On the page itself the links work fine, its brilliant.
However when I am on a different controller/view, The links do not take me back to the main controller, and to the anchor point clicked.
here is an example of one anchor link, which is in the header (which is on ALL controller views)
<%= link_to '#main', :id => 'menu_link' do %>
<li>Menu</li>
<% end %>
That is in :controller => "main", :action => "index"
When I am in another controller, for example my locations controller,
The links become like this localhost:3000/locations#menu
It should really be localhost:3000/#menu
The root is set to go to the main controller and index action.
Here is my routes.rb file
root :to => "main#index"
match 'admin', :to => 'access#admin_index'
match 'locations', :to => 'ranch_locations#locations'
match ':controller(/:action(/:id))(.:format)'
You need to specify that it is going to a different controller.
<%= link_to '#main', :controller => "main", :action => "index", :id => 'menu_link' do %>
<li>Menu</li>
<% end %>
You should use root_path(:anchor => :main) instead of "#main"
link_to "Comment wall", profile_path(#profile, :anchor => "wall")
# => Comment wall
http://apidock.com/rails/ActionView/Helpers/UrlHelper/link_to
upd:
<%= link_to '#main', :id => 'menu_link' do %>
should be changed to
<%= link_to root_path(:anchor => :main), :id => 'menu_link' do %>

Rails route for button

I'm a rails newbie and a question on routes is confusing me.
On one of my pages, I have a form. In that form I allow the user to fill in some needed information and press a "submit" button.
I get:
No route matches {:action=>"inventory_test", :controller=>"test_types"}
I do have an action in the test_type controller for "inventory_test".
My confusion is that routes seem to be defined according to the REST model, such as /Users/edit/1. That's fine, but how does one create routes for things like buttons?
I may be naive, but it seems like if I tried to setup a route in the form:
match 'some/url' => 'controller#action'
then I'm essentially defining the action for the entire page. How do I define actions for elements on the page?
When this button is clicked, I want the action in the controller called. I'm looking for:
match "submit_button" => 'test_types#inventory_test'
I realize I'm likely misunderstanding the paradigm, so any education is greatly appreciated.
Code:
(Note that I haven't tested the form code yet, but hopefully you get the idea)
index.html.haml
%div
%table
%caption
Inventory Tests
%form
Inventory Run: %input {:type => 'text', :name=>'inventory_run'}
Inventory Class: %input {:type => 'text', :name=>'inventory_class'}
=button_to "Run Inventory Test", :action => 'inventory_test';
If you are submitting a normal form then this should help
The route match '/url' => "test_types#inventory_test" should work fine.
<%= form_for(#user, :url => "/url") do |f| %>
"Put your form code here"
<%= f.submit "Submit" %>
<% end %>
Revert back if any queries .
Edited as per code posted
%form{ :action => "inventory_test", :method => "post"}
%label{:for => "inventory_run"} Inventory Run:
%input{:type => "text", :name => "inventory_run"}
%label{:for => "inventory_class"} Inventory Class:
%input{:type => "text", :name => "inventory_class"}
%input{:type => "submit", :value => "Submit"}
Just check, it should work for you but i have not tried with it

Simple link_to_remote function - can't get it to work

After utilizing the great trial and error for over an hour along with dozens of tutorials and blogs posting examples, I still cannot get the simple ajax functionality to work. Here is what is in my partial:
<span id="friend">
<%= link_to_remote image_submit_tag("/images/add_as_friend.gif"), :url => {:controller => 'friends', :action => 'add', :id => id} %>
</span>
This is what is in my controller:
class FriendsController < ApplicationController
def add
unless params[:id].nil?
Friend.create(:user_id => #current_user.id, :friend_id => params[:id], :friend_type => 2)
end
end
end
This is what is in my add.rjs file:
page.replace_html 'friend', "A request to be friends with this player has been sent."
The image for the link comes up fine. When I click on it, however, nothing happens. I've checked the database and nothing is going on. I must be missing something, any thoughts?
It may be because you are using image_submit_tag rather than image_tag for the content of your link. image_submit_tag is for submitting forms so will have its own onclick behaviour which may override the onclick behaviour that will be added as part of the link_to_remote functionality.
I would try:
<%= link_to_remote image_tag("/images/add_as_friend.gif"),
:url => {:controller => 'friends', :action => 'add', :id => id} %>

Rails - dynamically adding to sortable_element

Am adding to a sortable list using Ajax, and to get the Scriptaculous effects to kick in after the add, the only way I have found is by re-executing sortable_element.
Can anyone suggest a better way of doing this, at the complete code is a hack:
><%= link_to_remote "Add",
:url => { :controller => "pages", :action => "add_fragment", :pid => pid, :index => index },
:complete => "eval(decodeURIComponent(#{sortable_element 'frag_list', :url => sort_frag_pages_path, :complete => visual_effect(:highlight, 'frag_list'), :handle => 'handle'}).gsub('//',''));" %>
the answer is to add this into the controller method called by the link_to_remote
page.sortable :frag_list

Rails link_to_remote degradation and url_for

I am trying to create a link to destroy and entry in the DB using AJAX but I also want it to function without JavaScript enabled
However the following code
<%=link_to_remote "Delete", :update => "section_phone", :url => {:controller => "phone_numbers", :action => "destroy", :id => phone_number_display.id }, :href => url_for(:controller => "phone_numbers", :action => "destroy", :id => phone_number_display.id)%>
produces the output
Delete
For some reason the url_for does not work properly and the href tag is set to #.
I do not know why this does not work since I am not notified of any errors in the log
Does anyone know why this could be?
Thank you
You're missing curly braces around the options{} hash, which :update and :url belong to, to separate them from the html_options{} hash that :href belongs to.
Try this:
<%=link_to_remote "Delete", {:update => "section_phone", :url => {:controller => "phone_numbers", :action => "destroy", :id => phone_number_display.id }}, :href => url_for(:controller => "phone_numbers", :action => "destroy", :id => phone_number_display.id)%>
That'll get the URL to show up as the href attribute of your link, but a GET request to your destroy action shouldn't delete it. You'll need something else (like what vrish88 suggests) so that you can make a GET request to the destroy action to get a form, then POST that form to actually delete the phone number.
I believe your looking for something like this: RailsCasts - Destroy Without JavaScript

Resources