How to add a v-on:change attribute on a select tag using tag helper in rails - ruby-on-rails-3

I'm trying to ad the v-on:change attribute on a select element generated with select_tag
Something like this
<%= select_tag "worker_id", options_from_collection_for_select(#workers, "id", "name"), prompt: 'Choose', class: 'form-control', data: { "v-on:change" => "getWorker()" } %>
Also tried
<%= select_tag "worker_id", options_from_collection_for_select(#workers, "id", "name"), prompt: 'Choose', {}, { class: 'form-control', "v-on:change" => "getWorker()" } %>
Still no luck.

Try:
<%= select_tag "worker_id", options_from_collection_for_select(#workers, "id", "name"), prompt: 'Choose', class: 'form-control', '#change': 'getWorker' %>
Or:
<%= select_tag "worker_id", options_from_collection_for_select(#workers, "id", "name"), prompt: 'Choose', class: 'form-control', 'v-on:change': 'getWorker' %>

The correct version was to use #change and =>
<%= select_tag "worker_id", options_from_collection_for_select(#workers, "id", "name"), prompt: 'Choose', class: 'form-control', '#change' => 'getWorker' %>

Related

Rails 3 Formtastic form not rendering

Here is my form view code:
<div class="row">
<%= semantic_form_for #new_athlete_sport, :remote => true, :html => { :class => "new_sport", :"data-type" => 'json', :id => '' } do |f| %>
<%= f.label "Sport" %>
<%= f.select :sport_id, Sport.all.collect { |sp| [sp.name, sp.id] }, {}, { class: "chosen", id: "" } %>
<br />
<%= f.submit %>
<% end %>
</div>
For some reason the <form> tag isn't showing up in the DOM, but every other fields show up..
Seems the syntax in form_for has some problem.
Try remove "id" and move "data-type" from this
<%= semantic_form_for #new_athlete_sport, :remote => true,
:html => { :class => "new_sport", :"data-type" => 'json', :id => '' } do |f| %>
to this
<%= semantic_form_for #new_athlete_sport, :remote => true, :data=> {:type=> 'json'}, :html => { :class => "new_sport"} do |f| %>

URL parameters appear in the address bar although using POST

I'm new to ROR but I face this problem:
in the index page I have
<%= button_to 'Yes', { :action => 'vote', :id => poll.id, :user_answer => 'yes' }, :method => :post %>
when user presses the "Yes button" the url that passed to the controller contains all the parameters explicitly to the user.
vote?id=1&user_answer=yes
in routes.rb I have:
match 'vote' => 'polls#vote', :via => :post
Any help is appreciated
edit: the entire index.html.erb
Polls
<% #polls.each do |poll| %>
<p>
<%= poll.question %>?
<%= button_to 'Yes', { :action => 'vote', :id => poll.id, :user_answer => 'yes' }, :method => :post %> (<%= poll.yes %>) /
<%= button_to 'No', { :action => 'vote', :id => poll.id, :user_answer => 'no' }, :method => :post %> (<%= poll.no %>)
</p>
<% end %>
The default method for button_to is post, so no need to explicitly specify it unless you want to use other verbs (get, put).
If you don't like the params appended to the url, you might want to use a form instead.
Or, just add the form hash to your button_to tag:
<%= button_to 'Yes', { :action => 'vote', :id => poll.id, :user_answer => 'yes' }, :form => {:data_type => <your choice (html, json)> %>

Activeadmin and Formtastic: form not responding to :size

I am trying to format a form and the text fields respond to some methods, and not others.
I can do things like:
f.input :name, :input_html => { :maxlength => 10 }
f.input :name, :input_html => { :disabled => true }
But if I try to do any of the following, they do not work:
f.input :name, :input_html => { :size => 10 }
f.input :name, :input_html => { :class => 'autogrow' }
f.input :name, :input_html => { :rows => 10, :cols => 10 }
When I try using :size, for instance, the generated html shows that size=10, but is not reflected in the actual form.
These were more or less pulled right from the Formtastic documentation on Github, which the Activeadmin documentation refers to.
I am not sure if your question is solved or not.
However according to Formastic Official WIKI, your code should work:
Customize HTML attributes for any input using the :input_html option.
Typically this is used to disable the input, change the size of a text
field, change the rows in a textarea, or even to add a special class
to an input to attach special behavior like autogrow textareas:
<%= semantic_form_for #post do |f| %>
<%= f.inputs do %>
<%= f.input :title, :input_html => { :size => 10 } %>
<%= f.input :body, :input_html => { :class => 'autogrow', :rows => 10, :cols => 20, :maxlength => 10 } %>
<%= f.input :created_at, :input_html => { :disabled => true } %>
<%= f.input :updated_at, :input_html => { :readonly => true } %>
<% end %>
<%= f.actions %>
<% end %>
https://github.com/justinfrench/formtastic
if your code doesn't work , please check out the error logs, or put more debug info to your erb file, to see if you r rails is running under production mode.
i had the same problem. i wanted a nested form for edit with custom text field size.this worked for me.
form do |f|
f.inputs "Header" do
cf.input :name, :input_html => { :class => 'some_style', :rows => 2, :style => 'width:50%'}
end
f.actions
end
so basically u have to create your own class or just work with the :style.
For nested form u can use this code
form do |f|
f.inputs "Header" do
f.has_many :name,:allow_destroy => true,:new_record => true do |cf|
cf.input :first_name, :input_html => { :class => 'some_style', :rows => 2, :style => 'width:50%'}
end
end
f.actions
end

Rails 3 Switch menu reload page when change data

The user can edit his account and has a menu to choose his community :
<%= form_for #user, :html => { :multipart => true } do |f| %>
<%= f.collection_select :community_id, Community.find(:all,
:include => :memberships,
:conditions => ['memberships.user_id = ? and memberships.role > ?', #user.id, '0' ]),
:id,
:name,
:style => "width: 200px;" %>
<% end %>
I would like to put this menu on the show page and give him the possibility to switch directly.
With rails 2, I used observe_field or :onchange => 'this.form.onsubmit()' and I don't know java…
<%= form_for #user, :remote => true do |f| %>
<%= f.collection_select :community_id,
Community.where('memberships.user_id = ? and memberships.role > ?', #user.id, '0').includes(:memberships), :id, :name,{},
:onchange => "this.form.submit();" %>
<% end %>
Changed 'this.form.onsubmit() to 'this.form.submit()

Ruby on rails 3 link_to controller and action

I know this is probably a pretty simple concept. I am trying to create a link to a controller and action. For example I have a link in my layout file to update a record when a link is clicked, so I need to be able to link to the controller and action. How would I accomplish this?
link_to "Label", :controller => :my_controller, :action => :index
See url_for.
Also with CSS:
<%= link_to "Purchase", { :controller => :transactions, :action => :purchase }, { class: "btn btn-primary btn-lg", style: "width: 100%;" } %>
If you want to pass params too then do
<%= link_to student.name, controller: "users", action: "show", id: student.id, partial: "profile" %>