Calling another controller inside a form in Ruby - ruby-on-rails

In my app I have a case where a user can save a bill of lading from some form, or, send the same object to another controller.
My form is like this:
<%=content_for :detail do%>
<%=form_for #bill_of_lading,:url => bill_of_ladings_path(),:html=>{:class=>"form-horizontal bol-form"} do |f|%>
<%= render :partial => 'bill_of_ladings/form', :locals => {:f => f} %>
<div class="form-actions">
<%= button_to 'Create Draft', {:controller => "drafts", :action => "create", :bill_of_lading => #bill_of_lading }, :method=>:post ,:class=>"btn btn-warning"%>
<%=f.submit "Create Bill of Lading", :class=>"btn btn-primary","data-loading-text"=>"Saving..."%> or <%=link_to "cancel",bill_of_ladings_path,:class => "btn"%>
</div>
<% end %>
<% end %>
I want that, when the user press the "Create Draft" button, the data was sent to the DraftsController, the create method, passing the #bill_of_lading object.
But when I pressed the "Create Draft" button, it goes to the default submit button (in this case, to the bill_of_laddings_path).
So, how can I do the Create Draft button works as I want? (calling another controller, but passing the data as parameter)
Thanks

Related

Is it possible to make a link that functions as same as form does?

Now I have 2 forms that submit a comment.
Form Type A
<%=form_for(([#community, #comment]), :remote => true, :class => 'form' ) do |f| %>
<%= f.text_field :body, :id => "body", :class => "chat" %>
<button type="submit" class="btn">submit</button>
<% end %>
Form Type B
<%=form_for(([#user, #comment]), :remote => true, :class => 'form' ) do |f| %>
<%= f.text_field :body, :id => "body", :class => "chat" %>
<button type="submit" class="btn">submit</button>
<% end %>
Now, I want to have link_to button that functions as same as those forms do if a user clicks it.
When the user click on the link, #comment will be automatically filled just like below.
From Form Type A
#comment = "this is for community"
From Form Type B
#comment = "this is for user"
How can I do that? As far as I understand my situation.
Form is put type, then link_to is get type so it's impossible to re-use transaction of form.
Not sure what you mean by "transaction of form" but if you're asking if you can create/modify data via a single button or link than the answer is Yes, it is possible.
You can actually put with a link_to in rails ({:method => :put} (http://api.rubyonrails.org/classes/ActionView/Helpers/UrlHelper.html#method-i-link_to)
If you want a button to do this you should checkout button_to (http://api.rubyonrails.org/classes/ActionView/Helpers/UrlHelper.html#method-i-button_to)
It's better to use button_to.

How can I update the content of certain column without any page loading?

User has_one UserProfile.
Then UserProfile has wanted_message column as string.
Here, I'm showing input box to update wanted_message
I want it to be updated if certain user inputs any message then press "Update" button.
How can I? now, It's taking me to the url "/users/12" if I press "Update"
I don't want that:( I want it to be updated without any page loading(Ajax Request).
Then I want to have Action called update_message in User contoroller
How can I?
<%= form_for(current_user, :url => {:controller => "user", :action => "update_message" }, :remote => true, :class => 'form-search') do |f| %>
<%= f.fields_for :user_profile do |profile_form| %>
<div class="input-append">
<%= profile_form.text_field :wanted_message, :class => 'span6' %>
<button type="submit" class="btn">Update</button>
</div>
<% end %>
<% end %>
You have to set remote => true in your form and also set :method => put Put lets you update columns in your database and remote true will configure the form to send an ajax request. You'll also have to configure the update_message action in your controller to handle ajax requests. Finally, make sure your routes are configured correctly. You'll have to define that route in routes.rb and probably do an :as => 'update_message' to have access to that route helper method
This may help you with ajax in rails forms http://edgeguides.rubyonrails.org/working_with_javascript_in_rails.html
Here's a sample form, it's in haml though:
= link_to "Start", polls_toggle_live_path(poll.id), :method => :put, :remote => true, :class=> 'btn btn-success btn-small start-poll', :id => poll.id
Which links to this controller action
def toggle_live
#poll = Poll.find(params[:poll_id])
#poll.toggle_live
#poll.save!
end
Calls this method in the associated polls model to switch the db column value
def toggle_live
self.is_live = !self.is_live
end
Finally its configured in routes.rb this way to pass along the correct updates
match '/polls/toggle_live/:poll_id' => 'polls#toggle_live', :via => :put, :as => 'polls_toggle_live'

Rails : Linking an anchor from a different controller/view

<%= link_to (:controller => "company_stuff", :action => "index", :anchor => :menu), :class => 'links' do %>
<li>Terms of Use</li>
<% end %>
I am having difficulty linking a page which is on a different controller and also the link is an anchor. Basically the controller is called company_stuff the action is index and the anchor is called #terms
The problem was that the :controller :action :anchor was not being passed through as a hash, separate from the CSS class
Below is the solution
<%= link_to "Terms Of Use", {:controller => "company_stuff", :anchor => "terms"}, :class => "links" %>
I believe you can try something like this
<%= link_to index_company_stuff_path + "#terms", :class => 'links' do %>
<li>Terms of Use</li>
<% end %>
Or
<%= link_to index_company_stuffs_path + "#terms", :class => 'links' do %>
<li>Terms of Use</li>
<% end %>
Depending on your controller name and route.
You can find more information on this question How to create an anchor and redirect to this specific anchor in Ruby on Rails

ruby on rails passing an instance variable from view to controller

I have the follwoing form in my view :
I have an instance variable #selected_folder somewhere above in this view
<%= form_for :workflow_selection, :remote => true, :method => "get", :url => {:action => "final_submission"} do |f| %>
<p> Check the workflows needed and then click on the button with folder name</p>
<% #workflow_map[#selected_folder].each do |i| %>
<p><%= f.check_box(i)%> <%= f.label(i, i)%><br /><p>
<% end %>
<br />
<p><%= f.submit #selected_folder%></p>
<% end %>
I want to label the submit button as just 'submit' and should still be able to pass the #selected_folder instance variable to the final_submission action mentioned in the form_for tag
I tried various option like
<%= form_for :workflow_selection, :remote => true, :method => "get", :selected_folder => #selected_folder
:url => {:action => "final_submission"} do |f| %>
i tried to create a select drop down and hide it from view but still trying it to pass once the submit button is clicked.
and some more options..
None of them worked
Please help.
If you want to pass #selected_folder along in the form submission, you can add a hidden_field_tag.
As per Rails documentation:
hidden_field_tag(name, value = nil, options = {})
So in your case
<%= hidden_field_tag 'selected_folder', #selected_folder %>
in the workflow_selection, selected_folder will be present in the form hash.

Rails - I need a checkbox to change a field in the DB

I know I have done this before, but for the life of me I can't figure it out.
I have a table with a "called" field in it. I need to use a checkbox to update the db and check if "called" is "true" or not. Doesn't need to be AJAX, just needs to update the field.
table: rsvp
field: called
Thanks a ton.
A simple approach without ajax could be using a checkbox inside a form and submitting the form with the checkbox javascript onclick event.
Example:
View:
<% form_for #rsvp, :id => "rsvp" do |f| %>
<%= f.check_box :called, :onclick => "$('#rsvp').submit()" %>
<% end %>
this if you are using JQuery... with prototype the onclick string will be:
$('rsvp').submit()
Controller:
#rsvp = Rsvp.find(params[:id])
if #rsvp.update_attributes(params[:rsvp])
# success
else
# fail
end
Reference:
check box
In view:
<% form_for :rsvp, :url => {:controller => "rsvp", :action => "update"} do |f| %>
<%= f.check_box :called %>
<%= f.submit "Update" %>
<% end %>
In rsvp controller, update method:
#RSVPobject.updateAttribute(:called, params[:rsvp][:called])
If you just want to do this just by clicking the checkbox, you need to go the Ajax road.
Try using an "observe_field" in your view.
<%= observe_field ":called",
:frequency => 0.25,
:update => 'feedback_to_user',
:url => {:action => :mark_as_called},
:with => 'called',
:on => 'click' %>
All the details here.
Don't forget to adjust your routes so that the "mark_as_called" action can be found.

Categories

Resources