I am using rails 3.2.8 and I am having an issue with form_for:
I have the following form within the view of a different model:
<%= form_for pair, { :method => :put } do |f| %>
<td><%= pair.token.value %></td>
<td><%= pair.pair_token.value %></td>
<td><%= pair.freq %></td>
<td><%= pair.distance %></td>
<td><%= f.select :distance, [['', nil], ['Strongly Opposite', -3], ['Moderately Opposite', -2], ['Weakly Opposite', -1],
['No Relationship', 0], ['Weakly Similar', 1], ['Moderately Similar', 2], ['Strongly Similar', 3]], {}, {} %></td>
<td><%= pair.agree %></td>
<td><%= f.select :agree, [['', nil],['True', 1], ['False', 0]], {}, {} %></td>
<td><%= f.submit %></td>
<% end %>
This form is within the view for a model called tokens. The page is actually the tokens show.html. I am getting the response:
No route matches [POST] "/pairs/269671"
I am not sure what is going wrong as I am asking for the PUT action and the pair object is trying to do a post.
Thanks
What does your routes file look like?
Rails has default HTTP verbs that it uses for different actions. See here:
http://guides.rubyonrails.org/routing.html#crud-verbs-and-actions
Create happens via POST, and update happens via PUT. So, your routes need to support both, which is what Rails does in the background via resources :pairs in config/routes.rb.
You shouldn't need to, if you're using default Rails conventions, override which HTTP verb is used for a given form submission.
And, if you want further insight into these issues, take a look at this SO question:
PUT vs POST in REST
try this out
<%= form_for pair do |f| %>
<td><%= pair.token.value %></td>
<td><%= pair.pair_token.value %></td>
<td><%= pair.freq %></td>
<td><%= pair.distance %></td>
<td><%= f.select :distance, [['', nil], ['Strongly Opposite', -3], ['Moderately Opposite', -2], ['Weakly Opposite', -1],
['No Relationship', 0], ['Weakly Similar', 1], ['Moderately Similar', 2], ['Strongly Similar', 3]], {}, {} %></td>
<td><%= pair.agree %></td>
<td><%= f.select :agree, [['', nil],['True', 1], ['False', 0]], {}, {} %></td>
<td><%= f.submit %></td>
<% end %>
form_for knows how to handle create/update of the record, so there is no need to specify method.
Maybe you need to include the token in the form like:
<%= form_for [:tokens, pair] do |f| %>
<td><%= pair.token.value %></td>
<td><%= pair.pair_token.value %></td>
<td><%= pair.freq %></td>
<td><%= pair.distance %></td>
<td><%= f.select :distance, [['', nil], ['Strongly Opposite', -3], ['Moderately Opposite', -2], ['Weakly Opposite', -1],
['No Relationship', 0], ['Weakly Similar', 1], ['Moderately Similar', 2], ['Strongly Similar', 3]], {}, {} %></td>
<td><%= pair.agree %></td>
<td><%= f.select :agree, [['', nil],['True', 1], ['False', 0]], {}, {} %></td>
<td><%= f.submit %></td>
<% end %>
So your routes will be:
[POST] /tokens/:token_id/pairs/:pair_id
If that doesn't work try this one:
<%= form_for [#token, pair] do |f| %>
<td><%= pair.token.value %></td>
<td><%= pair.pair_token.value %></td>
<td><%= pair.freq %></td>
<td><%= pair.distance %></td>
<td><%= f.select :distance, [['', nil], ['Strongly Opposite', -3], ['Moderately Opposite', -2], ['Weakly Opposite', -1],
['No Relationship', 0], ['Weakly Similar', 1], ['Moderately Similar', 2], ['Strongly Similar', 3]], {}, {} %></td>
<td><%= pair.agree %></td>
<td><%= f.select :agree, [['', nil],['True', 1], ['False', 0]], {}, {} %></td>
<td><%= f.submit %></td>
<% end %>
Just don't hard code method cause same form is being used in different actions and you are breaking Rails conventions.
#object = MyModel.new
<%= form_for #object do |f| %>
same if you doing edit action
#object = MyModel.find(2)
<%= form_for #object do |f| %>
rails will take care about method. Hope it helps !
Related
currently image is only returning a String. I learned that this is what is causing the error from this question: Paperclip exception : Paperclip::AdapterRegistry::NoHandlerError
Here are the current parameters that I'm seeing:
Parameters:
{"utf8"=>"✓",
"_method"=>"put",
"authenticity_token"=>"SlvvynUVZCqV4fC3L8Dp1UM9IhDeihko44CFVyamxMU=",
"examples"=>{"66"=>{"image"=>"Ambition flyer 2 (1).jpg",
"description"=>"THis is a photo and what not"},
"67"=>{"description"=>""}},
"commit"=>"Submit",
"collection_id"=>"19"}
How do I get :image to return something like this in params (from other post):
"asset"=>
{"image"=>
#<ActionDispatch::Http::UploadedFile:0x000000056679e8
#content_type="image/jpg",
#headers= "Content-Disposition: form-data; name=\"asset[image]\";
filename=\"2009-11-29-133527.jpg\"\r\nContent-Type: image/jpg\r\n",
#original_filename=""2009-11-29-133527.jpg"",
#tempfile=#<File:/tmp/RackMultipart20120619-1043-yvc9ox>>}
**Currently, I have this in **edit_individual.html.erb****
<h2>Editing <%= #collection.title %> Collection</h2>
<%= form_tag update_individual_collection_examples_path, :method => :put do %>
<% for example in #examples %>
<%= fields_for "examples[]", example do |f| %>
<h2></h2>
<%= render 'pic-upload', :html => { multipart: true }, :f => f, :example => example %>
<% end %>
<% end %>
<p><%= submit_tag "Submit" %></p>
<% end %>
and this in my _pic-upload.html.erb partial:
<table>
<tr><td><%= image_tag example.image.url, size: "300x300" %></td>
<td><%= f.label :image, "Upload picture" %></td>
<td><%= f.file_field :image %></td>
<td><%= f.label :description, "Description (Optional)" %></td>
<td><%= f.text_area(:description, size: "50x3") %></td>
</tr>
</table>
<% end %>
Solution
Add :multipart => true the respective form_tag:
<%= form_tag(update_individual_collection_examples_path, :method => :put, :multipart => true)
Syntax is important here. The form_tag must include (parenthesis around the methods).
Resource
http://www.saalonmuyo.com/2010/01/27/using-form_tag-in-ruby-on-rails/
I have an articles model in my Rails 3 application. The article model has a column called categories which is set using a select box in the new form view. (It's a select box because the options should never change and there are only four of them).
The index view code I have is as follows:
<% #articles.category.each do |article| %>
<%= article.category %>
<% #articles.each do |article| %>
<tr>
<td><%= article.title %></td>
<td><%= article.author %></td>
<td><%= article.category %></td>
<td><%= link_to 'Show', article %></td>
<td><%= link_to 'Destroy', article, confirm: 'Are you sure?', method: :delete %></td>
</tr>
<% end %>
<% end %>
I have grouped by category in my controller:
#articles = Article.group(:category).order('title ASC')
However, this results in an exception which points to the following line <% #articles.category.each do |article| %>.
What is the tidiest (from within my view code) way of achieving the following:
Category 1
Article 1
Article 2
Category 2
Article 5
Category 3
Article 8
So each article is listed under it's category.
I'd suggest you to use group_by method (Documentation):
# in your controller
articles = Article.order('title ASC')
#grouped_articles = articles.group_by &:category
# in your view
<% #grouped_articles.each do |category, articles| %>
<%= category %>
<% articles.each do |a| %>
<tr>
<td><%= a.title %></td>
<td><%= a.author %></td>
<td><%= link_to 'Show', a %></td>
<td><%= link_to 'Destroy', a, confirm: 'Are you sure?', method: :delete %> </td>
</tr>
<% end %>
<% end %>
I want to know how to get the selected value of <select>. I only know how to populate this.
Here's is my code in index.html.erb. I used this to populate the <select> dropdown menu.
<h1>Trap</h1>
<%= form_for #search do |f| %>
<p>
Employee Code:
<%= f.select(:empcode_contains, #employee.collect {|e| [ e.empcode, e.id ]}) %>
</p>
<p class="button"><%= f.submit "Search" %></p>
<% end %>
<p>
Sort by:
<%= sort_link #search, :empcode %> |
<%= sort_link #search, :date_entry %> |
</p>
<table>
<tr>
<th>Empcode</th>
<th>Date entry</th>
<th></th>
<th></th>
<th></th>
</tr>
<% #traps.each do |trap| %>
<tr>
<td><%= trap.empcode %></td>
<td><%= trap.date_entry %></td>
<td><%= link_to 'Show', trap %></td>
<td><%= link_to 'Edit', edit_trap_path(trap) %></td>
<td><%= link_to 'Destroy', trap, :confirm => 'Are you sure?', :method => :delete %></td>
</tr>
<% end %>
</table>
<br />
<%= link_to 'New Trap', new_trap_path %>
And in my controller traps_controller.rb:
def index
#search = Trap.search(params[:search])
#traps = #search.all
#employee = Employee.all
Trap.all.each do |t|
Employee.create(:empcode => t.empcode)
end
respond_to do |format|
format.html # index.html.erb
format.xml { render :xml => #traps }
end
end
Pls tell me how to get the selected value if the user selects a value. I don't have any idea.
Try (in the controller) :
value = params[:empcode_contains]
or you could fetch the whole object this way:
search = params[:search]
I have a model called PointOfContact I created with a scaffold; so it should have some semblance of correctness about it.
Basically, I want an alias for my routes. Instead of going to '/point_of_contacts', I want '/pocs' and I don't want '/point_of_contacts' to be a valid route.
I have tried this:
resources :pocs, :controller => "point_of_contacts"
That works to create the '/pocs' route. However, now I am unsure as to how my Views should be written.
Specifically:
<% #point_of_contacts.each do |point_of_contact| %>
<tr>
<td><%= point_of_contact.first %></td>
<td><%= point_of_contact.last %></td>
<td><%= point_of_contact.title %></td>
<td><%= point_of_contact.phone %></td>
<td><%= point_of_contact.email %></td>
<td><%= link_to 'Show', point_of_contact %></td>
</tr>
<% end %>
That code creates this exception:
No route matches {:action=>"show", :controller=>"point_of_contacts", :id=>#<PointOfContact id: 1, system_id: nil, first: "Tester", last: "Test", title: "", phone: "", email: "", created_at: "2011-03-10 20:03:21", updated_at: "2011-03-10 20:03:21">}
Try resources :point_of_contact, :path => "/pocs"
The portion of the view that is applicable:
<% #projects.each do |project| %>
<tr>
<td><%= project.name %></td>
<td><%= project.description %></td>
<td><%= link_to 'Show', project %></td>
<% if can? :update, #project %>
<td><%= link_to 'Edit', edit_project_path(project) %></td>
<% end %>
<% if can? :destroy, #project %>
<td><%= link_to 'Destroy', project, :confirm => 'Are you sure?', :method => :delete %></td>
<% end %>
</tr>
<% end %>
models/ability.rb
class Ability
include CanCan::Ability
def initialize(designer)
can :read, :all
end
end
This is the error I get:
NameError in Projects#index
undefined local variable or method `current_user' for #<ProjectsController:0x000001016d62d8>
Extracted source (around line #18):
15: <td><%= project.description %></td>
16: <td><%= link_to 'Show', project %></td>
17:
18: <% if can? :update, #project %>
19: <td><%= link_to 'Edit', edit_project_path(project) %></td>
20: <% end %>
21:
Thoughts?
I had errors in my AuthLogic install. Or rather, not errors, but when I installed it I used current_designer (which was the main user I was concentrating on) rather than current_user.
It seems CanCan didn't like that.
So I am now in the process of re-doing all my user models. But so far, it seems to have fixed this issue.