This form to update a work item does not work anymore.
The form is generated in the erb file using a this command:
<%= form_for(#work_item, :url => admin_workitem_update_path) do |f| %>
The generated tag looks like this:
<form accept-charset="UTF-8" action="/admin/workitem/define/14" class="edit_workitem" enctype="multipart/form-data" id="edit_workitem_14" method="put">
</form>
The route looks like this:
admin_workitem_update PUT /admin/workitem/define/:workitem_id(.:format)
Follow-up: using :method => :post in the form tag works.
Why does rails generate the put method attribute?
NEVER use equal sign befor form. I have the same problem with Instant Rails implementation. The tutorial on rails documentation should be used without <&=
<%= form_for(#work_item, :url => admin_workitem_update_path) do |f| %>
use insteand:
<% form_for(#work_item, :url => admin_workitem_update_path) do |f| %>
this is all
Related
im new to ruby on rails, abit confused between the usage of the following when i try to update a record:
<%= form_for #article, url:{action: "update"} do |form| %>
this one works, but i dont understand how come the submit button says 'update article'
<%= form_for :article, url:{action: "update"} do |form| %>
this one throws No route matches [POST] "/articles/2", and the submit button says 'save article'
finally:
<%= form_with(model: #article) do |form| %>
actually updates the record, but i dont understand why it's calling update, and not other methods
form_for(#article) creates a form builder which is bound to a model instance.
If #article is nil it will raise an error.
If the instance is a new record the form will use method="POST" and action="/arcticles".
If the record has been persisted it will have method="PATCH" and action="/arcticles/:article_id".
Rails derives the URL for the action attribute based on convention over configuration. So there is no need to explicitly pass the url option if you follow the conventions.
An example of this would be:
<% #article = Article.new(title: 'Hello World') %>
<%= form_for(#article) do |f| %>
<%= f.text_input :title %>
<% end %>
This will render something like:
<form action="/articles" method="POST">
<input type="text" name="article[title]" value="Hello World"/>
...
</form>
<%= form_for #article, url:{action: "update"} do |form| %> this one
works, but i dont understand how come the submit button says 'update
article'
The form builder knows it is updating an record by calling .new_record? on the the record you passed to form_with. You can change the default value of the submit button by providing translations:
# config/locales/en.yml
en:
helpers:
submit:
create: "Save new record"
update: "Save changes"
form_for(:article) creates a scoped form builder that does not wrap an object.
This creates a form builder where the inputs will be "scoped". For example:
<%= form_for(:article) do |f| %>
<%= f.text_input :title %>
<% end %>
This will render something like:
<form action="/articles" method="POST">
<input type="text" name="article[title]"/>
...
</form>
Rails derives the URL for the action attribute based on convention over configuration.
In your case <%= form_for :article, url:{action: "update"} do |form| %> causes a routing error since form_for defaults to method: "POST".
form_with is the Rails 5.1 replacement for form_for and form_tag
form_with will replace the form_for and form_tag methods which are closely related yet have very different signatures. form_for and form_tag have been soft depreciated and are slated for removal.
The idea is to provide a single method with a more consistent signature.
If you are using Rails 5.1+ this is what you should be using.
See:
Rails Guides - Action View Form Helpers
Rails API - ActionView::Helpers::FormHelper
Rails 5.1's form_with vs. form_tag vs. form_for
It all depends on #artical. If #artical is new object (id in #artical is nil) is call the create action. If #artical is existing object then it called the update method.
My code is working on local server but it's not working on the production server. I can't figure it out what I'm doing wrong.Please help me.
This is where I'm getting error in my partial:
<%=form_for #shiftchange, :url => { :controller=>"schedulers" ,:action => "shift_change" },:validate=>true , :method => :post do |f|%>
<%= f.label :from_date , "From Date " %>
<%= f.text_field :from_date ,:class =>'datepicker' %>
<% end %>
To load the partial,this is what I'm doing this:-
<%= render "schedule_shift" %>
In the controller I have this:
#shiftchange = Shiftchange.new
If the form is included for multiple actions (pages) you need to set #shiftchange to something or the form_for directive will fail (it won't have anything to create a form for).
A much better approach (and the Rails default) is to have separate views for each action, each including only the elements required by that action, and loaded into your application.html.erb layout with <%= yield %>. So you would have a app/views/shiftchanges/new.html.erb view which has the form in it. You never very rarely need to define any load paths in Rails, they are all derived from the model, controller and action names - or from your routes.rb. This is a core part of the convention over configurationhttp://en.wikipedia.org/wiki/Convention_over_configuration paradigm which runs so deep in Rails.
If you do need to have a form for creating a new object on every page (often used for ShiftchangeSessions for example), you can rewrite your form_for so that it doesn't depend on an object being present:
form_for(Shiftchange.new)
or if you need to force it to post to the create method
form_for(Shiftchange.new, :url => { :action => "create" })
You can read more about resource driven form_for in the Ruby On Rails API docs http://api.rubyonrails.org/classes/ActionView/Helpers/FormHelper.html#method-i-form_for-label-Resource-oriented+style.
Change
form_for #shiftchange
for this
form_for :shiftchange
i guess it works
I have been trying to create a multi-part form in Rails for the last day, which is crazy, but I am really not sure how to get around this one.
Currently, here is the code in my view:
<%= form_for #account, :html => {:multipart => true } do |f| %>
However, the HTML returned is as follows:
<form accept-charset="UTF-8" action="/accounts/1" class="edit_account" id="edit_account_1" method="post">
For the life of me I can't figure out why the form is not showing up as a multi-part. This particular form is used to upload an image using paperclip to AWS, but fails each time, presumably because it isn't a multipart form.
Help! :) And thanks.
Hi according to Rails API v3.1.3, your code should look like following:
<%= form_for #account,{},:html => {:multipart => true } do |f| %>
The difference is by passing empty options to rails helper and it will read your html parameters.
Thanks
UPDATE:
Here is the code copied from one of my projects:
It is working and runs under Rails 3.1
May be you could try to put brackets after the "form_for"
<%= form_for(#account,{},:html => { :id=>"account_form",:multipart => true }) do |f| %>
<%= render :partial => "form", :object => f %>
<%= f.submit 'create' %>
<% end %>
This worked for me.
<%= form_for(#account, html: { :multipart => true }) do |f| %>
or
<%= form_for(#account, html: { :enctype => 'multipart/form-data' }) do |f| %>
As per #peterpengnz's answer, providing an empty {} parameter to form_for got ArgumentError:
wrong number of arguments (3 for 1..2)
It turns out that I'm a huge idiot, and the original form was working fine, EXCEPT...
I was rendering the form in a partial, but I wrapped the partial in a standard, non-multipart form tag, which overwrote the multipart form, and somewhat surprisingly didn't raise any errors.
Either way, I am a stupid one for not noticing this, but it is now resolved and the file uploading is working perfectly.
I have the following form in my Rails application:
<% form_tag password_resets_path, :id => 'recoverPasswordForm' do %>
<label for="passwordRecoveryEmailAddress">Email Address:</label>
<%= text_field_tag "passwordRecoveryEmailAddress" %>
<%= submit_tag 'Recover' %>
<br />
<div id="forgotPasswordLoginLinkContainer">
<a id="forgotPasswordLoginLink" href="/login">Login Instead</a>
</div>
<% end %>
When this form is submitted, the page must reload. I would like to easily turn this form into an AJAX form, such that the form submits via AJAX, and a page reload does not happen.
I could do this easily using jQuery, hooking into the .submit() function. But, I am curious: does Rails provide some easy way to turn any given form into an AJAX form? Or, what's the simplest (yet elegant) way possible? Maybe something like
<% form_tag password_resets_path, :id => 'recoverPasswordForm', :ajax => true do %>
I'm using Rails 2.
Yes, and you were close with your guess. Rails 3 allows you to do form_tag ..., :remote => true to let the form use AJAX if Javascript is available.
See http://railsapi.com/doc/rails-v3.0.0/classes/ActionView/Helpers/FormTagHelper.html#M002483
I am using rails3 beta3 and couchdb via couchrest. I am not using active record.
I want to add multiple "Sections" to a "Guide" and add and remove sections dynamically via a little javascript. I have looked at all the screencasts by Ryan Bates and they have helped immensely. The only difference is that I want to save all the sections as an array of sections instead of individual sections. Basically like this:
"sections" => [{"title" => "Foo1", "content" => "Bar1"}, {"title" => "Foo2", "content" => "Bar2"}]
So, basically I need the params hash to look like that when the form is submitted. When I create my form I am doing the following:
<%= form_for #guide, :url => { :action => "create" } do |f| %>
<%= render :partial => 'section', :collection => #guide.sections %>
<%= f.submit "Save" %>
<% end %>
And my section partial looks like this:
<%= fields_for "sections[]", section do |guide_section_form| %>
<%= guide_section_form.text_field :section_title %>
<%= guide_section_form.text_area :content, :rows => 3 %>
<% end %>
Ok, so when I create the guide with sections, it is working perfectly as I would like. The params hash is giving me a sections array just like I would want. The problem comes when I want edit guide/sections and save them again because rails is inserting the id of the guide in the id and name of each form field, which is screwing up the params hash on form submission.
Just to be clear, here is the raw form output for a new resource:
<input type="text" size="30" name="sections[][section_title]" id="sections__section_title">
<textarea rows="3" name="sections[][content]" id="sections__content" cols="40"></textarea>
And here is what it looks like when editing an existing resource:
<input type="text" value="Foo1" size="30" name="sections[cd2f2759895b5ae6cb7946def0b321f1][section_title]" id="sections_cd2f2759895b5ae6cb7946def0b321f1_section_title">
<textarea rows="3" name="sections[cd2f2759895b5ae6cb7946def0b321f1][content]" id="sections_cd2f2759895b5ae6cb7946def0b321f1_content" cols="40">Bar1</textarea>
How do I force rails to always use the new resource behavior and not automatically add the id to the name and value. Do I have to create a custom form builder? Is there some other trick I can do to prevent rails from putting the id of the guide in there? I have tried a bunch of stuff and nothing is working.
Thanks in advance!
Ok, I think I have figured out something that works. Changing the first line of the partial to:
<%= fields_for "sections", section, :index => "" do |guide_section_form| %>
Seems to work just fine. This way both new and edit form looks the same under the hood and the params hash works just like I need it to. If anyone sees anything wrong with this or has another alternative, please let me know.