OK, I'm totally new to Ruby on Rails. I created a form that posts to an external widget that returns JSON. So I have this form:
<%= form_for :email, :url => 'http://XXX.XX.XXX.212/widgetapi.0.1.php', :html => {:class => "new_email"} do |f| %>
<%= f.text_field :email, :value => "Your email address...", :class => "text", :id => "email", :name => 'email',
:onFocus => "change(this,'#222222'); this.value=''; this.onfocus=null;",
:size => "26" %>
<%= f.hidden_field :apiKey, :id => "apiKey", :name => 'apiKey', :value => "ABC123" %>
<%= f.hidden_field :lrDomain, :id => "lrDomain", :name => 'lrDomain', :value => "signup.triplingo.com" %>
<%= f.hidden_field :urlPrefix, :id => "refCodeUrl", :name => 'refCodeUrl', :value => "http://signup.website.com/" %>
<%= f.hidden_field :ref_code, :id => 'ref_code', :name => 'ref_code', :value => #referralid %>
<%= submit_tag "Enter To Win", :class => "button-positive submit" %>
<% end %>
Which works. Now I get back a JSON response that is:
({"email":"testing#testing2.com","reflink":"fi1ts","newuser":true})
Ok now the result it the browser sits on the response page with the JSON.
I'm guessing I need to do something with the #response in the controller, but I'm not sure what. All I want to do is if that "newuser" is true, provide them a success page. If false, go to an error page.
Thanks.
You should change that form, so that it will send request to your controller. In that controller you should do API Call with parameters from a form(using for example Curb: https://github.com/taf2/curb or Net/http: http://www.ruby-doc.org/stdlib/libdoc/net/http/rdoc/index.html). Then You can parse result in JSON and show user correct page.
So:
1) User send request to YOUR application
2) YOUR application make request to http://XXX.XX.XXX.212/widgetapi.0.1.php using data from user
3) YOUR application receives JSON, and check if newuser is true.
4) If it is true action render success page, otherwise it render error page.
Related
Any idea on what could be wrong in this form?
Error:
vehicleTrack.html.erb:141: syntax error, unexpected keyword_do_block, expecting => ... params[:rangefrom_string]} do |f| #output_buffer.safe_appe... ... ^
This is my view:
<%= simple_form_for '', url: convertTrackToArea_path, :method => :post,
{ :controller => "vehicles",
:action => "convertTrackToArea",
:search => params[:search],
:rangefrom_string => params[:rangefrom_string]} do |f| %>
<%= f.input :areano, :label => 'Areano' %>
<%= f.button :submit, value: "Crear",:name => nil%>
<% end %>
I have not used simple_form so my guess is you are passing the last last argument incorrectly and it wont take a hash as argument and it detects the argument as key and searching for a value so throws error that => is missing, which is used to identify value in hash. So you can do something like this I suppose:
<%= simple_form_for '',
:url => url_for(:action => 'convertTrackToArea', :controller => 'vehicles',:search => params[:search],
:rangefrom_string => params[:rangefrom_string]),
:method => 'post' do |f| %>
since you are specifying controller and action you dont need to mention convertTrackToArea_path . If that route is already setup, you can just use that like in your posted question and remove controller and action name like:
<%= simple_form_for '', :url => convertTrackToArea_path(:search => params[:search],:rangefrom_string => params[:rangefrom_string]),:method => 'post' do |f| %>
My rails _form code:
<%= simple_form_for #admin_event, :html => { :class => 'main-form' } do |f| %>
<%= f.input :time_of_event, :format => l(:time_of_event, '%d %b. %Y'), :as => :string, :label => 'Hora', :input_html => { :class => 'thin', :placeholder => 'eg: 20:30' } %>
<% end %>
I'm getting the error message:
Cannot convert symbol to string.
How can I set up this _form to always display this field using a time converter so while the database has a full field (2000-01-01 19:30:00.000000), in the forms it would only show 19:30?
Solved this with the following: created the method "time_format" on my ApplicationHelper:
def time_format(datetime)
datetime.strftime('%H:%M') unless datetime.blank?
end
And then on the form:
<%= f.input_field :start_time, :as => :string, :value => time_format(f.object.start_time) %>
Hope this will help you.
You can add attr_accessor to your model (for example formatted_time) and use this field for get fromatted time and set it. Before save you can parse value of #formatted_time and apply it to time_of_event field.
I have this form in a view in my project. I need to pass the task_id to a certain controller, but the log does not seem to be receiving the parameters. I don't know what the problem is.
<%= form_for :taskid, :url => {:action=>"index", :controller=>"statistics"}, :html => {:class => "nifty_form", :method => "GET"} do |f| %>
<%f.hidden_field :task_id, :value => task.id%>
<td><%= f.submit "اختر مهمة لاظهار احصائياتها منفرده"%></td>
<% end %>
You are missing on = after <%. The equal sign is needed whenever you want to the result appears on the HTML, so it is used with the field tags methods or render, for instance. You should not use the equal when using a if, for example, because this is not what you want to print (well, it can be, but most likely it isn't)
<%= form_for :taskid, :url => {:action=>"index", :controller=>"statistics"}, :html => {:class => "nifty_form", :method => "GET"} do |f| %>
<%= f.hidden_field :task_id, :value => task.id%>
<td><%= f.submit "اختر مهمة لاظهار احصائياتها منفرده"%></td>
<% end %>
However, as #AntonGrigoriev pointed out, you should use a object if you have, like this
<%= form_for #task, :url => {:action=>"index", :controller=>"statistics"}, :html => {:class => "nifty_form", :method => "GET"} do |f| %>
or you can simply use the hidden_field_tag
<%= hidden_field_tag :task_id, task.id %>
Hi please test with following code to send hidden value in rails, I have tried and worked for one of my application :
hidden_field_tag(name, value = nil, options = {}) public
eg:
<%= hidden_field_tag(:field_name,value=#offer_status)%>
I want to make a Search form where user can search others tasks based on country, city, location, gender, age, date of birth, etc. Here is my search form...
<%= form_for(#othertask, url: "/other_task", :html =>{:method => :post}) do |f| %>
<%= select_tag "country", options_for_select([], params[:country]), {:multiple => false, :class => "countries", :'data-country' => "IN", :id => "countries_states1"} %>
<%= select_tag :state, options_for_select([], params[:state]), { :class => "states", :'data-country' => "countries_states1", :'data-state' => ""} %>
<%= text_field_tag :city, params[:city], :placeholder => "City"%>
<%= text_field_tag :dob, params[:dob], :placeholder => "date of birth", :id => "dp2" %>
<%= select_tag :highlyeducation, options_for_select(["ME/MTech","MCom","MCA","BE/BTech","MBA","BCA/BSc","BCom"], params[:higheducation]), {:multiple => false} %>
<%= radio_button_tag :gender,'Male', params[:male] %>Male
<%= radio_button_tag :gender,'Female', params[:female] %>Female <br/><br/>
<%= f.submit "Search", class: "btn btn-large" %>
Here is my controller --
def create
if #other_tasks = Micropost.joins(:user).where('users.city' => params[:city], 'users.country' => params[:country])
render 'index'
else
#other_tasks = []
render 'index'
end
end
A user can search by filling zero, one, two or all fields. For this If i make all possible combination to fetch all tasks it takes lots of queries to write. How can i fetch tasks based on user input by using only one query.
I didn't understand how tasks are connected to Microposts in your domain and why do you use the create action in order to search for tasks. I think clarification of these matters will help you get a better answer.
What you need to do is to group all the searchable fields into an array and apply the filter whenever the value received for that field in params is not blank:
def create
#other_tasks = Micropost.joins(:user)
search_fields = [:country, :state, :city, :dob, :highlyeducation]
search_fields.each do |f|
#other_tasks = #other_tasks.where("users.#{f} = ?", params[f]) unless params[f].blank?
end
render :index
end
I want to preset a textarea with a value but it isn't working.
<%= f.text_area(:self_summary, :input_html => { "date-pre" => "I will get to this later."}, :class => "textareastyle") %>
<%= f.text_area(:self_summary, :value => "I will get to this later.", :class => "textareastyle") %>