button_tag not send commit - ruby-on-rails

I need use button_tag instead submit_tag on a form to add icon with style http://twitter.github.com/bootstrap/base-css.html#buttons.
<%= simple_form_for(bla..........bla......)) do %>
<%= button_tag t('.sent_to_trash'), :class => "btn btn-small btn-primary disabled", :id => "trash_button", do %>
<i class="icon-trash icon-white"></i>
<%= t('.sent_to_trash') %>
<% end %>
<% end %>
The question is I can not receive the params[:commit] with button_tag, however with submit_tag is working fine and I receive correctly params[:commit] on my action controller.
How can I fix this problem?
Thank you very much!

Html element button works with :name and :value params, so you have to explicitly define these, e.g.
= button_tag(:name => "commit", :value => "my_button") do
= "Press me!"
then you get params[:commit] = "my_button" after form submit.
Note: You should specify :type attribute too, because different browsers use different default types for the <button> element (:type => "submit")

Related

Rails form_for sending params to controller action and not model

In the controller review_queue I have a custom action that posts a result to a target URL, I want to build a form for this action. I am not going to save any of the fields to the DB I am just going to pass them in the params to the post_review action.
def post_review
RestClient::Request.execute(:method => :post,
:url => Rails.application.secrets['target_url'],
:content_type => :json,
:payload => #result_params.merge!(params[:reasons]).to_json,
:headers => HEADERS)
end
In the view I have a form that will be filled out and on submit it should send up the reasons when the form is submited, I am setting the review_queue_id and the status in the form, since these are static, but the reasons should come from the textarea
<%= form_for(:review_queue, url: { action: 'post_review', :review_queue_id => #review_queue.id, :status => 'accepted'} ) do |f| %>
<div class='form-group'>
<label for='comment'>Please give a reason? (required)</label>
<%= f.text_area(:reasons, placeholder: 'Your commentns ...', rows: 9, class: 'form-control') %>
</div>
<div class='modal-footer'>
<%= f.submit 'Approve', class: 'btn btn-success btn-decission btn-modal-left-side' %>
<button type='button' class='btn btn-default' data-dismiss='modal'>Close</button>
</div>
<% end %>
error message:
NoMethodError - undefined method `reasons' for #<ReviewQueueApplication:0x007fa7ff7832d8>:
It seems as if rails is assuming the MVC architecture here, and assuming I want to pass the reasons to the review_queue model. there is no reasons column so it's dropping a no method error. Is there a way of specifying that the form is 'temporary' and only getting as far as the controller?
This seems like it should be a simple thing but there is some rails magic happening here.
NoMethodError - undefined method `reasons' for
ReviewQueueApplication:0x007fa7ff7832d8
form_for assumes that you are creating a form for a model object and expects the fields to be present in that specific model's table(in a normal situation).
You should be going with form_tag
<%= form_tag post_review_path, method: :get, :review_queue_id => #review_queue.id, :status => 'accepted'} ) do |f| %>
<div class='form-group'>
<label for='comment'>Please give a reason? (required)</label>
<%= text_area_tag(:reasons, placeholder: 'Your commentns ...', rows: 9, class: 'form-control') %>
</div>
<div class='modal-footer'>
<%= submit_tag 'Approve', class: 'btn btn-success btn-decission btn-modal-left-side' %>
<button type='button' class='btn btn-default' data-dismiss='modal'>Close</button>
</div>
<% end %>
And in the controller access it like params[:reasons]. Also if you noticed, I've added method: :get to the form_tag as you don't want to save the info to DB
The rails helper form_for is used for forms for rails resources. You want to use the form_tag helper. Search for form_for and form_tag here for more information on these 2 methods.

How to construct a clickable link with a scope using Ransack and Rails

I have the requirement to have some scopes as clickable links in my application. This will allow the user to change the data they are seeing as required. Using Ransack and it's ransackable_scopes functionality I am very close. I do need to retain any filtering Ransack has done when the user clicks the scope.
I've got the scopes working but now I just need to construct the clickable link.
Here's my model:
class Product < ActiveRecord::Base
scope :upward_trending, -> { where( "status > ?", 100).where(above_revenue_average: true).order('end_date DESC') }
scope :downward_trending, -> { where( "status < ?", 100).order('end_date DESC') }
def self.ransackable_scopes(auth_object = nil)
[:upward_trending, :downward_trending]
end
end
Now in my view I've added these two scopes as hidden fields, like so:
<%= search_form_for #q, :html => {:class => 'filter-form'} do |f| %>
<div>
<%= f.hidden_field :upward_trending %>
<%= f.hidden_field :downward_trending %>
<%= f.label :name_cont, "Search", class: 'label' %>
<%= f.search_field :name_cont, class: 'form-control input-box', :placeholder => 'Search' %>
</div>
<div>
<%= f.submit "Filter", class: 'btn btn-primary' %>
<%= link_to "Clear Search", request.path, class:"btn btn-default" %>
</div>
<% end %>
From here I just need to create the links and it should work.. What is the best way to do this?
Thanks for your help!
I was planning to do dirty way. (But haven't yet)
create search_form_for every scope (you will have 2 form for your case)
set hidden field with own criteria (as you do in your code but each in own form)
make submit button looks like link (with css I think it is not very difficult. You can see Bootstrap button appeared as link)
Not very clean or elegant.
I achieved this by creating hidden fields for each scope and then creating a button with onclick javascript:
<%= f.hidden_field :upward_trending %>
<%= button_tag(:type => 'submit', :class => 'btn btn-primary scope-button upward_trending', :id => "upward_trending", :onclick => "document.getElementById('q_downward_trending').value = 0; document.getElementById('q_upward_trending').value = 1;") do %>
<i class="fa fa-chevron-up fa-2x"></i><br>Upward<br>Trending
<% end %>

html_safe not working with rails

I can't manage to make it work... even using raw or html_safe
view
<%= button_to(glyphicon('heart', 'I love it !'), some_path, class: "btn btn-success")%>
helper
def glyphicon(glyph, text = nil)
html = "<i class=\"glyphicon glyphicon-#{glyph}\"></i>"
html += " #{text}" if text
html.html_safe
end
result
A success btn with <i class= and " /> after
The following works (I've been doing it for ages), but the extra do syntax is annoying...
<%= button_to(some_path(#etude), class: "btn btn-success") do %>
<i class="glyphicon glyphicon-heart"></i> I love it !
<% end %>
EDIT
Found a more compact syntax :
<%= button_to(some_path(#etude), class: "btn btn-success"){
glyphicon('heart', 'I love it')} %>
I don't think its a problem with html safe, rather the name attribute for button_to will only accept plain text.
<%= button_to("<b>hello</b>".html_safe, 'http://www.google.com', :class => 'button', :method => 'get') %>
and
<%= button_to("<b>hello</b>", 'http://www.google.com', :class => 'button', :method => 'get') %>
both produce a button with <b>hello</b> written on it rather than just hello in bold.
The method using do is the only way to do it, it's the same thing you need to do when linking glyphicons via link_to.
But you could do this.
<%= button_to(some_path(#etude), class: "btn btn-success") do %>
glyphicon('heart','I love it')
<% end %>

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.

Rails passing information from a view to a form

I have a Rails app that includes Requests and Workorders tables.
I have a New Workorder button on the Request show page. I need to pass information from the Request to the Workorder - for instance the Request.id.
I'm currently using flash to do this. Here is the button on the Request show page:
<%= link_to 'New Work Order', new_workorder_path, :class => 'btn btn-primary', :onclick => (flash[:request_id] = #request.id %>
In the new Workorder form, I have:
<% if flash[:request_id] != nil %>
<%= f.hidden_field :request_id, :value => flash[:request_id] %>
This works. But, not always. And I haven't been able to figure out why it fails sometimes.
Is there a better way to pass this data?
Thanks for the help!!
UDPDATE1
Sometimes I need to bring forward quite a few data fields. For example:
<%= link_to 'Follow-up Work Order', new_workorder_path, :class => 'btn btn-primary', :onclick => ( flash[:workorder_id] = #workorder.id, flash[:client_id] = #workorder.client_id, flash[:contact_id] = #workorder.contact_id, flash[:location_id] = #workorder.location_id, flash[:type_id] = #workorder.type_id, flash[:woasset_id] = #workorder.woasset_id) %>
You can try passing the parameter to the path of the link and then pass it to the form via your controller's action:
Link:
<%= link_to 'New Work Order', new_workorder_path(request_id: #request.id), :class => 'btn btn-primary' %>
Controller
def new
#request_id = params[:request_id]
...
end
In your view:
<%= f.hidden_field :request_id, value: #request_id %>

Resources