Rails views to get variable from controller - ajax

I'm using form with :remote => true for ajax functionality, this form works fine but outside this form I have select_tag for other feature :
<%= select_tag "months", options_for_select(#dynamic_month.collect { |name|
name }, 'ALL'), id: 'chart_filter', :onchange =>"monthWise(this)" %>
Here I'm passing #dynamic_month from controller, but I'm not getting updated value from controller after submission of form, Is there any way we can achieve this keeping this select option outside the form and getting updated data from controller.
Thank you.

Related

form_tag calling an instance of a class on the controller

I am trying to use a form_tag on a Rails application that, when submitted, call an instance of a class on the controller.
View
form_tag '/order', method: :get
select_tag 'type', options_for_select(#options_for_type, #type)
text_field_tag 'numbers', nil, placeholder: "100"
submit_tag "Get Info"
controller
def order
#order = Order.new(params_here).fetch_info
#type = params[:type] || "type1"
#options_for_type = [["type1", "type1"], ["type2", "type2"]]
...
end
This class sends some API requests (JSON).
Now problem is, I am getting an error from the API that some parameters are missing, which is expected since I haven't submitted the form with all the params, but it seems the class is called automatically when the page is loaded. I would have expected the class to be called only when the submit button is pressed?
Can you make the form_tag sort of a block? Like:
form_tag '/order', method: :get do
# All form elements
end
Maybe displaying your route here can also help.

redirect to another URL using submit button in ruby

I have submit button and i want to redirect in another URL (hard coded) this URL
https://www.ccavenue.com/shopzone/cc_details.jsp
my code :
<%= form_tag #ccavanue do |f| , url => "https://www.ccavenue.com/shopzone/cc_details.jsp", :html => :method => "put" %>
<%= hidden_field_tag :my_field, #MerchantId, :id => 'merchant_id' %>
<%= submit_tag "Click Me" %>
<% end %>
i want to redirect another website URL with this submit button . please guided me.
Change your code to following:
<%= form_for #ccavanue, url: "https://www.ccavenue.com/shopzone/cc_details.jsp" do |f| %>
<%= f.hidden_field :my_field, #MerchantId, :id => 'merchant_id' %>
<%= f.submit "Click Me" %>
<% end %>
In Rails a form is designed to create or update a resource and reflects the identity of the resource in several ways:
The url that the form is sent to (the form element's action attribute) should result in a request being routed to the appropriate controller action (with the appropriate :id parameter in the case of an existing resource),
Input fields should be named in such a way that in the controller their values appear in the appropriate places within the params hash, and
For an existing record, when the form is initially displayed, input fields corresponding to attributes of the resource should show the current values of those attributes.
In Rails this is achieved by creating form using form_for where:
If we want to create any object we use POST method within url and PUT method if we are trying to update an existing record.
Rails framework is smart enough to use POST or PUT method by itself looking at the url of the form. So in this case we need not use method parameter within form_for url.
Probably you can start with Michael Hartl's tutorial

Submit a form via Ajax with Simple Form and Rails 4

I'm using Rails 4.0.1, Simple Form and Bootstrap 3 to create a modal dialogue for creating a new record. I'm trying to submit the form via ajax, but I am unable to do so.
I've added :remote => true to the form declaration, as well as to the submit button. Yet the request is still being sent via HTML POST, causing a full-page reload.
The form is created as:
<%= simple_form_for SomeClass.new, :authenticity_token => true, :remote => true, :html => {:class => 'form-horizontal'} do |f| %>
And I've tried to create the submit button with:
<%= f.submit :data => {:remote => true}, :class => "control-button" %>
Neither of these work. The generated HTML shows the proper data-remote attributes. Yet elsewhere in my app, I've been using standalone links with link_to together with the :data-remote => true option, which work via ajax as expected. Turbolinks is disabled. But I'm simply having trouble submitting the form with ajax.
Am I overlooking something? What is a suitable alternative?

how to use pass paramters to controller every time an item is selected in select tag in rails 3?

this my select statement, I guess I cannot figure out how to make it work:
<%= select("post", {project_id: #project, word_id: #word_id},
#words[count].synonyms.all.collect {|syn| [ syn.name ] ,
{:id=>"Menu1_Id", :class => "Menu1_Class", :onchange => "alert('hey')"}) %>
#project and #word_id are the parameters I want to make it pass to the controller,
where to add my url path ?

Rails: Ajax-enabled form without a model object

I'm new to Rails and having a hard time figuring out how to create a form that submits over Ajax without having a corresponding model object.
My use case is a simple form that collects an email address and sends it an email; there's nothing to be persisted, so no model.
In cases where I do have a model, I've had success with form_for(#model, remote: true). I can't seem to find the right helper for the case where there is no model. I tried form_tag(named_path, remote: true) and that works, but does not use Ajax.
Pointers to an example with an example with a proper controller, .html.erb and routes.rb would be really appreciated.
Here's how I solved it. The key was using form_tag and specifying a hash with an argument that properly matched my route. For my business logic, I need to pass in an "id" param, which is used in the controller logic.
<%= form_tag(sendmail_path({ id: 1}), remote: true) do %>
<%= text_field_tag :email, params[:email] %>
<%= submit_tag "Send", remote:true %>
<% end %>
routes.rb:
MyApp::Application.routes.draw do
post '/sendmail/:id(.:format)' => 'mycontroller#sendmail', :as => 'sendmail'
# etc...
end
I also had to supply a sendemail.js.erb template which updates the form after submission.

Resources