Ruby on Rails: only get, put, and delete allowed - ruby-on-rails

My view:
<h1>New Address</h1>
<% form_for #address, :url => new_address_path do |f| %>
<%= f.error_messages %>
<%= render :partial => "form", :object => f %>
<%= f.submit "Add Address" %>
<% end %>
Partial... nothing special
<%= form.label :number %><br />
<%= form.text_field :number %><br />
<br />
<%= form.label :street %><br />
<%= form.text_field :street %><br />
<br />
<%= form.label :city %><br />
<%= form.text_field :city %><br />
<br />
<%= form.label :state %><br />
<%= form.text_field :state %><br />
<br />
<%= form.label :zip_code %><br />
<%= form.text_field :zip_code %><br />
<br />
<br />
my error:
ActionController::MethodNotAllowed
Only get, put, and delete requests are allowed.

MethodNotAllowed seems to come from resource-based routing. Resource-based routing requires distinct method names to match actions. In your case, you should supply :method => :post in form_for, something like that:
<% form_for #address, new_address_path, :method => :post) do |f| -%>

Related

Can't verify CSRF token authenticity

Hi sir in rails 4 while using form_for also getting above error in google i read about this and follow that but even also i am getting same error
if i disable in application controller and added protect_from_forgery with: :null_session it is working but when i submit the form_for form i am getting params missing or params should not empty .
i refered this link i refered linkeven also not working
Error in the log file
Processing by UsersController#create as HTML
Can't verify CSRF token authenticity
code view
<%= form_for #user do |f| %>
<p>
<%= f.label :first_name %><br />
<%= f.text_field :first_name %>
</p>
<p>
<%= f.label :last_name %><br />
<%= f.text_field :last_name %>
</p>
<p>
<%= f.label :gender %><br />
<%= f.text_field :gender %>
</p>
= f.fields_for :address do |p| %>
<p>
<%= p.label :city %><br />
<%= p.text_field :city %>
</p>
<p>
<%= p.label :state %><br />
<%= p.text_field :state %>
</p>
<p>
<%= p.label :country %><br />
<%= p.text_field :country %>
</p>
<p>
<%= p.label :postal_code %><br />
<%= p.text_field :postal_code %>
</p>
<% end %>
<p><%= f.submit %></p>
<% end %>
if set protect_from_forgery with: :null_session and if submit the form
than i will getting one more error
param is missing or the value is empty:
userActionController::ParameterMissing (param is missing or the value is empty: user):

Activeadmin form - Rails

How I can make this form in activeadmin?
<%= form_for(#album, :html => {:multipart => true}) do |f| %>
.....
<div class="field">
<%= f.label :apellido %><br />
<%= f.text_field :apellido %>
</div>
<div class="field">
<p>Hijos</p>
<%= f.fields_for :hijos do |builder| %><br /><br />
<%= builder.label :nombre, 'Nombre Hijo' %><br />
<%= builder.text_field :nombre %><br />
<%= builder.label :apodo, 'Apodo Hijo' %><br />
<%= builder.text_field :apodo %><br />
<%= builder.label :hijo_id, 'favorito' %>
**<%= f.radio_button :hijo_id, builder.object.id %>**
<% end %>
</div>
I need put the option of hijo_id inside the for of :hijos
Try with :
f.input :avatar_item_id, :as => :boolean, :value => app_f.object.id
But not work.
Thanks
This should work once you register the Album model as an ActiveAdmin resource:
form :html => {:multipart => true} do |f|
f.inputs "Principal" do
f.input :apellido
end
f.inputs "Hijos" do
f.has_many :hijos do |h|
h.input :nombre
h.input :apodo
h.input :favorito, :as => :check_box
end
end
f.buttons
end
If you want to mark a child as a favorite, you need a boolean favorito field in the hijos table, not a hijo_id field.

Set HTML id attribute in Rails form select box

How do I set the id attribute in my drop down boxes?
Here is the part of my form in question:
<%= f.fields_for :items do |builder| %>
<%= builder.label :description %><br />
<%= builder.text_field :description %><br />
<%= builder.label :material %><br />
<%= builder.select :material, #letters.map { |l| [l.material, l.material] }, :id => "material_field" %><br />
<%= builder.label :height %><br />
<%= builder.select :height, #letters.map { |l| [l.height, l.height] }, :id => "height_field" %><br />
<%= builder.label :thickness %><br />
<%= builder.select :thickness, #letters.map { |l| [l.thickness, l.thickness] }, :id => "thickness_field" %><br />
<%= builder.label :quantity %><br />
<%= builder.text_field :quantity, :id => "quantity_field" %>
<%= builder.link_to_remove "Remove this item" %>
<% end %>
The :id => "quantity_field" method works for text fields, but not for the select fields. Viewing the HTML source I am getting an id of "estimate_items_attributes_0_material" for the material text box.
This is a strange inconsistency. Any help will be greatly appreciated.
There is a parameter between the possible choices and the html options. So you have to do this :
<%= builder.select :thickness, #letters.map { |l| [l.thickness, l.thickness] }, {}, :id => "thickness_field" %>
You can find the doc here : http://api.rubyonrails.org/classes/ActionView/Helpers/FormBuilder.html#method-i-select
And this one can also be helpful :
http://api.rubyonrails.org/classes/ActionView/Helpers/FormOptionsHelper.html#method-i-select

Problem with Ruby Gem Paperclip

I am having trouble using the Ruby Gem paperclip. I followed the instructions in the ReadMe but I cannot seem to get it to actually load my images. Here is my edit form:
<% form_for :user, #user, :html => { :multipart => true } do |f| %>
<div class="field">
<%= f.label :name %><br />
<%= f.text_field :name %>
</div>
<div class="field">
<%= f.label :email %><br />
<%= f.text_field :email %>
</div>
<div class="field">
<%= f.label :expertise %><br />
<%= f.text_area :expertise, :class => "expertise" %>
</div>
<div class="field">
<%= f.label :occupation %><br />
<%= f.text_field :occupation %>
</div>
<div class="field">
<%= f.label :city %><br />
<%= f.text_field :city %>
</div>
<div class="field">
<%= f.label :state %><br />
<%= f.text_field :state %>
</div>
<div class="field">
<%= f.label :password %><br />
<%= f.password_field :password %>
</div>
<div class="field">
<%= f.label :password_confirmation, "Confirmation" %><br />
<%= f.password_field :password_confirmation %>
</div>
<%end%>
Yet when I try to save I keep getting this error: No route matches "/users/4/edit"
What is the problem
The error is telling you that there's no /users/4/edit route. What does your config/routes.rb look like? If there's a line like:
resources :users
Then, try changing that first line to:
form_for #user
Instead of:
form_for :user, #user
Also, I don't see a file_field in there anywhere so I don't think this question is about Paperclip?

How can I add a dropdown/select using RoR helper?

I have a from like this:
<% form_for(#user) do |f| %>
<%= f.error_messages %>
<p>
<%= f.label :username %><br />
<%= f.text_field :username %>
</p>
<p>
<%= f.label :email %><br />
<%= f.text_field :email %>
</p>
<p>
<%= f.label :password %><br />
<%= f.password_field :password %>
</p>
<p>
<%= f.label :password_confirmation %><br />
<%= f.password_field :password_confirmation %>
</p>
<p>
<%= f.label :role %> <br/>
<%= f.text_field :role%>
</p>
<p>
<%= f.submit 'Create' %>
</p>
<% end %>
In the database, role is a "Char" field. I want it different from a textfield, the user can select "Teacher", "Student", if the user select "Teacher", the database will store "T", otherwise, it will store "S". How can I do so? It is necessary for me to add a "User role" table in the database, and then make a relationship with the user? But it is necessary to do it this way? thank u.
Ref select and options_for_select
<%= f.select :role, options_for_select([["Teacher", "t"], ["Student", "s"]]) %>

Resources