Routing problem with calling a new method without an ID - ruby-on-rails

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

Related

Rails Form_tag syntax error

I get an error when I run this code. I want to make a button that redirects to an action from pages_controller.
Submultimi.html.erb
<%= form_tag({:controller => '/pages_controller', :action => 'calculeaza'}, :method => "post") do %>
<%= text_field_tag :field1 %>
<%= submit_tag "Button" %>
<% end %>
pages_controller.rb
def Submultimi
end
def Combinari
end
def Permutari
end
def calculeaza
puts "YAY"
redirect_to '/combinari'
end
Error message: No route matches {:action=>"calculeaza", :controller=>"pages_controller"}
routes.rb
Rails.application.routes.draw do
get '/submultimi' => 'pages#Submultimi'
get '/combinari' => 'pages#Combinari'
get '/permutari' => 'pages#Permutari'
end
If you get an error you must include the error message in the question. Anyway the controller name is obviously wrong so this must be the problem. The controller should not include the "/" nor the "_controller".
<%= form_tag( { :controller => 'pages', :action => 'calculeaza' }, :method => "post") do %>
<%= text_field_tag :field1 %>
<%= submit_tag "Button" %>
<% end %>
Your routes are also wrong:
there's no calculeaza method in routes
methods should be lowercase in routes and controller
Thanks to Phlip for the correction about the controller name :)
As Pablo said, remove the / from your controller name.
Your error message says there is no route defined. That means you haven't correctly told rails what to do with your form's post request; it's trying a route that doesn't exist.
You've got a few things going wrong. Your action names are capitalized in routes.rb, but your method names (at least the one you've linked) is not. They're case sensitive, convention is all lowercase. Also, you don't have a route defined for calculeaza. You need one, in routes.rb add (something like, I haven't tested any of this):
post '/calculeaza/' to 'pages#calculeaza'
If you want to see your currently defined routes, run rails routes in a terminal, and to use it in code append _path to the prefix verb. You end up with something like:
form_tag calculeaza_path do
You may want to read the rails routing guide, especially the parts about resourceful routes.

Rails Routing and link_to

It's a little late here so maybe this a trivial question where I'm missing something simple. But when I click a button (with link_to) I created the following gets appended to my URL:
%23<ActionView::Helpers::FormBuilder:0x3ef1fd8>
Why is this, and how can I prevent this? Again, I apologize if this is a shallow question. I can post more information regarding routes and whatnot if that is needed.
Thanks!
Edit: More information as requested.
View:
<%= link_to "Index", welcome_path(f), :class => 'button' %>
with f being part of a form_for loop. I think I'm passing the wrong parameter but I'm unsure.
Relevant Route:
get "index" => 'welcome#show', :as => 'index'
Update:
Thanks for the help everyone. I ended up getting it working by pluralizing my controller (I don't know why I didn't have that before) and utilizing welcome_url instead. That seemed to do the trick.
Check out the very first example and paragraph in the Rails API docs for ActionView::Helpers::FormBuilder:
<%= form_for #person do |f| %>
Name: <%= f.text_field :name %>
Admin: <%= f.check_box :admin %>
<% end %>
What this is saying is that f represents an instantiated FormBuilder object that you are passing to the welcome_path method in your link_to helper.
Typically, you would not mix #index and #show in your routes. Depending on what you want to use the WelcomesController for, you might actually want to route your root_path to welcome_index:
get "welcome/show" => 'welcome#show', :as => 'welcome'
root 'welcome#index'
You should run: $ rake routes in the terminal to get an idea of path view helpers that you can use in your app.
Maybe you're trying to send users to a personalized welcome page. You could have something like this for your corresponding link_to helpers would look best like this:
<%= link_to "Show", welcome_path(#user.id), :class => 'button %>
<%= link_to "Index", root_path, :class => 'button' %>

Click pictures and update database row on rails

I need to be able to click an image (out of a bunch of images) and update a profile table based on the image clicked.
List of images in my view:
<% #pages_selection.each do |pages_selection| %>
<img>
<%= link_to image_tag(pages_selection.page_picture, '#') %>
</img>
<% end %>
Then I've got a method in my controller, called save_new_scores_to_profile, that averages the data from the picture and updates the profile values.
How do I call my controller method when my link_to (the image) is clicked? Is there something like this available?
if link_to clicked
perform_controller_or_helper_method
end
Because the user needs to select multiple images, I want them to stay on the page after clicking the images (that's why I have the link directed to '#'. I also have a submit button at the end of the page if that helps.
I'm open to using something other than link_to.
EDIT:
Here's where I'm at now in routes
resources :preferences do
member do
get 'save_new_scores_to_profile'
get 'checked_average_with_profile'
end
end
and the view:
<% #pages_selection.each do |pages_selection| %>
<img>
<%= link_to image_tag(pages_selection.page_picture, :controller =>
:checked_average_with_profile, :action => :save_new_scores_to_profile, :image_id
=> pages_selection.id) %>
</img>
<% end %>
I have functions checked_average_with_profile and save_new_scores_to_profile in my controller that I know work (after testing with helper functions).
link_to image_tag(pages_selection.page_picture, :controller => :my_controller, :action => :save_new_scores_to_profile, :image_id => pages_selection.page_picture.id )
Insted of my_controller put the name of your controller.
With the :image_id you have passed a parameter that you can reference in your controller action with the params hash like: params[:image_id].
Do all your work in that controller action (or with additional calls of helper methods),
find the picture that has that image_id and make a redirect_to #picture_with_that_id

I got a Routing error when using form_tag in rails 3.0

I am new to rails. Today I encountered a problem that I have no clue how to fix it.
Basically I am trying to put a input area and a submit button on one webpage, and the input values is stored in the params[:name], passing to the export_issues method defined in issues controller.
this is what the view file looks like
<%= form_tag(:controller => 'issues', :action => 'export_issues') do%>
<p>
<%= label_tag :name, "name:" %>
<%= text_field_tag :name, params[:name]%>
</p>
<%= submit_tag "Submit" %>
when i click the 'Submit' I got "Routing error". But if I just press F5 refresh the error pageor type 127.0.0.1/issues/export_issues it will work just as I wanted
and this is the code related to issues controller in routes.rb
resources :issues, :only => [:index, :destroy] do
member do
post 'create_comment'
get 'mark_readed'
end
collection do
get 'export_issues'
delete 'destroy_comment'
end
end
basically what the export_issues does is to read the database and export data to a CSV file.
It worked fine without the form_tag codes taking part in.
So what is the problem ?
The reason is simple. By default form_tag creates form element with method=post. Like:
form_tag('/myposts')
will create form tag as follows:
<form action="/myposts" method="post">
However, our routing says, it accepts only get. So, the form opening tag should be:
<%= form_tag(:controller => 'issues', :action => 'export_issues', :method => :get) do%>
For more information, please consult the apidocs.

Select_tag submitted needs to redirect to show page with :id?

Hi
I have asked a question similar to this before but never got it resolved. So I am trying again.
This seems like it should be so simple. I am not using Rails 3 yet BTW.
All I want to do is have a drop down menu and when a person chooses that location and presses "go" they go to that page.
<% form_tag installation_path([:id]), :url => { :action => "show" }, :method => :get do %>
<%= select_tag :id, options_from_collection_for_select(Installation.find(:all), :id, :name) %>
<%= submit_tag 'Go' %>
<% end %>
This becomes the issue: http://localhost:3000/installations/id?id=1&commit=Create. It can't find the :id. I just don't know how to route this correctly. It seems like this shouldn't be that difficult.
Any help would be great. Thanks.
I think there might be a problem with your form_tag. It seems you're defining the path twice.
Both
installation_path([:id])
and
:url => { :action => "show" }
are used to generate the path but I don't think you should be using both. Just go with
installation_path([:id])
or
:url => { :controller => "installations", :action => "show", :id => id }
You need to create and use a new "show" route that is not based on the installation id (and doesn't collide with Rails resource routes), and continue to send the installation id into the controller's show action as part of the params object.
In routes.rb,
get 'show_installation', to: 'installations#show'
In your view,
<% form_tag show_installation_path, :method => :get %>
...

Resources