Rails - two forms on one page - ruby-on-rails

I have two forms on one view page. One of these is an update form. The other form is a single button that goes to the same controller action but does not actually update the form. Here is the first form:
<%= form_for [#project, #schedule] do |f| %>
<%= f.fields_for :tasks do |builder| %>
<%= render 'task_fields', :f => builder %>
<% end %>
<p><%= link_to_add_fields "Add task", f, :tasks %>
<p><%= f.submit "Submit" %></p>
<% end %>
And here is the second form:
<%= form_tag project_schedule_path(#project, #schedule), method: "patch" do %>
<div><%= hidden_field_tag :emp_accepts, true %></div>
<%= submit_tag "Accept schedule", class: "btn btn-large btn-primary" %>
<% end %>
The problem is the strong parameters. The strong parameters require :schedule, which only the first form provides. So when I try to use the second form, an error is returned. Here are the strong params:
def schedule_params
params.require(:schedule).permit(:emp_accepts,
tasks_attributes: [:title, :content, :_destroy])
end

you should change the second form to
<%= form_for [#project, #schedule], http: { method: 'patch' } do |f| %>
<div><%= f.hidden_field :emp_accepts, value: true %></div>
<%= f.submit 'Accept schedule', class: 'btn btn-large btn-primary' %>
<% end %>

It looks like you should really use different controller methods for each action. It would make it easy for you to make changes in the future.

Related

No method error occurs in rails while creating a new text_field

I am trying to add a text field via form and setting the name as code.
My form calls a action verify in users controller. But when I run it
shows no method error code. I am new to rails can anyone help. I need to pass code as a params. I already created a user sign up and signin page.
<%= form_for(#user, :action => 'verify') do |f| %>
<%= f.label :code %>
<%= f.text_field :code %>
<%= f.submit "Verify", class: "btn btn-large btn-primary" %>
// a action in usercontroller
def verify
end
The user model probably does not have a code field. So the application is giving an error. If you just want to send code, use form_tag instead of form_for.
<%= form_tag verify_action_path do %>
<%= label_tag :code %>
<%= text_field_tag :code %>
<%= submit_tag "Verify", class: "btn btn-large btn-primary" %>
<% end %>

Ruby Rails is it possible to check before displaying form?

Is it possible to check submission before showing this simple form:
<%= form_for #article, url: {action: "create"}, html: {class: "nifty_form"} do |f| %>
<%= f.text_field :title %>
<%= f.text_area :body, size: "60x12" %>
<%= f.submit "Create" %>
<% end %>
like if its already submitted first time show other html content instead ?
Test the object. If you're using ActiveRecord/Mongoid:
<% if #article.new_record? %>
... display form ...
<% else %>
... do something different ...
<% end %>

Changing button value of a scaffold when in /new and /edit in rails

I generated a Stories scaffold and now when I'm in stories/new, the button that I click says "Create Story." How do I change the value of that to say something else, like "Create Tale"?
I've gone into the stories/new.html.erb and also the stories/edit.html.erb, but all that is there is
<%= render 'forms' %>
When I head to stories/_form.html.erb there is a
<div class="actions">
<%= f.submit class: "btn" %>
</div>
I know you can put <%= f.submit "Create Tale", class: "btn" %>, but if I do that it will say "Create Tale" for both creating and updating. How could I make is also say "Update Tale" for when I'm in stories/edit?
For giving it a name, try:
<%= f.submit class: "btn", name: "Create Tale" %>
For naming it differently depending on the action. What I normally do is pass the button-name into the form-template from the action-view.
Eg in new.html.erb:
<%= render partial: 'form', button_name: 'Create Widget' %>
in edit.html.erb:
<%= render partial: 'form', button_name: "Update Widget" %>
in form:
<%= f.submit name: button_name %>
The alternative is not to put the submit buttons in the "form" partial, but to keep them in the action-views eg for "new.html.erb"
<%= form_for #widget do |f| %>
<%= render partial: 'form' %>
<%= f.submit name: 'Create Widget' %>
<% end %>
for "edit.html.erb"
<%= form_for #widget do |f| %>
<%= render partial: 'form' %>
<%= f.submit name: 'Update Widget' %>
<% end %>
NOTE: code not tested and probably buggy, but you get the drift...
work directly on the form (partial _form)
<%= f.submit "Submit", class:"btn"%>
For Rails 5, change:
in new and edit views
and
the same way.
Then in form use:
instead of

Rails 3 - keep params after submit

I want to keep my params afterr i do submit.
In my Rails 3.2.0 application i have something like that:
http://0.0.0.0:3000/journals?class_code=11v&subject_name=Math
And i have form:
<%= form_for #lesson, :html => {:class => "form-horizontal"} do |f| %>
<%= field_set_tag do %>
....
<%= f.submit "Create", :class => "btn btn-large btn-success" %>
<% end %>
<% end %>
I want to keep my params (class_code and subject_name) after f.submit. How can i do that?
To store all parameters in one field, you could use:
<%= hidden_field_tag :parameters, request.query_string %>
And then you can access them in controller, using:
parameters = parse_nested_query(params[:parameters])
hidden fields
....
<%= hidden_field_tag :class_code, params[:class_code] %>
<%= hidden_field_tag :subject_name, params[:subject_code] %>
<%= f.submit "Create", :class => "btn btn-large btn-success" %>
<% end %>
but - if those are attributes of your model, then assign them in the new action of the controller
def new
#lesson = Lesson.new(:class_code => params[:class_code], :subject_code => params[:subject_code])
end
# in this case the view code is slightly different
<%= f.hidden_field :class_code %>
<%= f.hidden_field :subject_code %>

Rails - Simple Form - Not loading class option

I'm having some troubles when I want to change the class of a form.
I have this partial:
<%= simple_form_for(#activity) do |f| %>
<%= f.input :name %>
<%= f.submit 'Guardar',:class=>"btn success" %>
<% end %>
In the browser the form label has class="simple_form activity"
According to Simple Form documentation, I can change the class by doing this
<%= simple_form_for(#activity, :defaults=>{:class=>"my_class"}) do |f| %>
But nothing is happening, what is wrong?
JavierQ
I think you want to use:
:html => { :class => "btn success"}
Instead of:
:class=>"btn success"

Resources