I am working with a rails form which takes users text input and sends it to the controller.
It sends two infos, the text and the language of the text (I18n.locale variable). My form looks something like that:
<%= form_for(:text, :url => {:action => 'create'} ) do |f| %>
<%= f.label :content, "#{t :"Write whatever you want"}" %><br />
<%= f.text_area :content, :cols => 80, :rows => 3 %> <br />
<%= f.hidden_field :locale, :value => I18n.locale %>
<%= f.submit "#{t :Post}"%>
<% end %>
I am sending that locale value using a hidden field. But I think this is a bad practice. User can easily modify this form. So is there any way to send that locale value among other form data automatically without any visible/hidden field?
If you want to avoid modifing data by user maybe you can use session like locale_session = "en", and use that session when you are dealing with data in controller
There are two ways of adding variable with rails form
<%= f.hidden_field :locale, :value => I18n.locale %>
AND
<%= form_for(:text, :url => {:action => 'create', :locale => I18n.locale} ) do |f| %>
But both of the above can easily modified by the user.
To avoid this either you have to use sessions or ssl page
Temporary solved this adding another line into controller. Not sure it it safe or not
def create
#text = Microblog.new(params[:text])
#text.update_attributes(params[#text.locale = "#{I18n.locale}" ])
Related
Value the variables written in variables #first_city, #last_city, #date_trip of the controller. Without creating an object in the database. Then pass the values of these variables to another page. It is possible to do that or not?
<%= form_for(#orders) do |f| %>
<p>Выберите маршрут</p>
<%= f.select(:first_city, #city_select, :value => :first_city, prompt: "Откуда") %>
<%= f.select(:last_city, #city_select, prompt: "Куда") %>
<%= f.text_field :date_trip %><br>
<%= f.submit "Дальше", class: "btn" %>
<% end %>
Yes.
However if you state
form_for(#orders)
it will automatically assume you are creating an Order.
However you can do
form_for #order, :url => {:action => "action"}
create your own action and access those values trough
order_params[]
And you can do whatever you want with that data
Hope i helped
I have been trying to create a form on a page, with an instance variable #next, but it doesn't seem to work. When I remove the part which includes the form, the page opens up, clearly depicting that the paths have been correctly mentioned in routes.rb. To be clear,i haev added the following method in the controller file
def new
#next=Self.new
end
The part where form is created.
<%= form_for(#next) do |a| %>
<%= a.text_field_tag :name %>
<%= a.text_field_tag :prof %>
<%= a.text_field_tag :pic, :placeholder => 'Enter address' %>
i m using Rails 4.0.2
routes.rb
get '/worms' => 'worms#index'
get '/worms/new' => 'worms#new'
post '/worms' => 'worms#create'
get '/worms/:id' => 'worms#show'
Please help.
Your form should be look like this as below...
Self means what ?, You should write the name of your model here.
And If you need full CRUD operation on the same then write the below line for the routes...
resources :your-model-name-in-plural
<%= form_for #next do |a| %>
<%= a.text_field :name %>
<%= a.text_field :prof %>
<%= a.text_field :pic, :placeholder => 'Enter address' %>
I've got a Rails app that has two main views: an overview and a sequenced view. The user can enter data in either view. I use the same form helper for both views, but I want the redirect_to from the create action to respect the user's context. So I pass a hidden_field_tag called 'track'.
I can't seem to access the value of 'track' inside my controller.
Here's the form:
<%= form_for([#mission, #mission.stickies.build]) do |f| %>
<div class="field">
<%= f.text_field :name, :size => 60 %>
<%= f.hidden_field :kind, :value => kind %>
<%= hidden_field_tag :track, :value => track %>
<class="actions">
<br><%= f.submit "Add to " + kind.pluralize.capitalize %>
</div>
<% end %>
And here's where I call it in one of the views:
<%= render :partial => "stickies/form" , :locals => { :kind => "driver", :track => 'main' } %>
Here's the parameters dump (from a different call):
{"utf8"=>"✓",
"authenticity_token"=>"N3IXwNQosOfxw1ZcpfFPOLPKzHbvNyaBhAiP3ftT9GY=",
"sticky"=>{"name"=>"Tesssksjd argghh.",
"kind"=>"success"},
"track"=>"{:value=>\"sequence\"}",
"commit"=>"Add to Successes",
"mission_id"=>"32"}
And here's the relevant code in my create controller:
if params[:track][:value] == "main" then
redirect_to mission_path(#mission) + '#' + #sticky.kind.pluralize
elsif params[:track][:value] == "sequence" then
redirect_to mission_stickies_path(#mission, :kind => #sticky.kind)
end
I can't seem to find the syntax, or comparator, or whatever I need to access the value represented by "track"=>"{:value=>\"sequence\"}".
Any help or pointers would be greatly appreciated. I'm new to Rails and Ruby, this is my first app.
Don't write it with the :value => track, rather do:
<%= hidden_field_tag :track, track %>
and access it with params[:track]
I have a link_to in my view which is going to a URL( query string) that is created dynamically in the controller.
<%= link_to "Search Venue", #venue_search, :target => :blank %>
and in the controller I'm pulling some attributes from a model and using those values in a query to hit Yahoo's API and
#event = Event.find(params[:id])
search_values = {
:api_key => 'xxxxxxxxx',
:search_text => #event.venue_name,
:location => #event.venue_zipcode,
:radius => 100
}
#venue_search = "http://yahooapis.com/rest/?method=venue.search&" + search_values.to_query
And everything is working perfect so far.
I would like to manually enter a few more parameters into the query and I'm just wondering what would be the best direction to go.
Is there a way to create a form which some text fields that I can use to insert parameters manually into the query string and to use the submit button to call the url as a link_to?
I was thinking something like
<% form_for #venue_search(:city, :state) do |f| %>
<%= f.text_field :city %>
<%= f.text_field :state %>
<% end %>
And some how add those two new parameters to the query and then execute the query
Is that possible?
If i properly understand your question, you can use for sending 'GET' request - in such case parameters will be placed in query:
<% form_for #venue_search, :method => :get do |f| %>
<%= f.text_field :city %>
<%= f.text_field :state %>
<% end %>
Hope its help you.
I've been looking at the new options available in HTML5 forms, such as declaring input types as "email", "url", and "number", as described here.
How can I use these in conjunction with the rails form builders? I have tried
<% form_for #user do |f| %>
<%= f.email :email, {:placeholder => 'user#domain.com'} %>
<% end %>
But that does not work. I've also tried
<% form_for #user do |f| %>
<%= f.text_field :email, {:placeholder => 'user#domain.com', :type => :email} %>
<% end %>
But the type is still "text" and not overridden. Is it possible, or is this something that will need to be addressed in Rails itself?
Looks like there is currently an open ticket and patch for adding the HTML5 form input types. If you can't wait until the patch is accepted, you could apply the patch locally by freezing the actionpack gems and applying the patch or making an initializer that add the extra methods.
Of course the other option is adding the fields manually without a form helper:
<% form_for #user do |f| %>
<%= tag(:input, {:type => :email, :value => f.object.email} %>
<% end %>
There is now an email_field tag.