Rails button to update status of nested attributes - ruby-on-rails

I have a model Project and a model ProjectLine. Project has nested attributes for ProjectLine.
On my projects show view I'm listing project info with list of tasks.
<% #project.project_lines.each do |project_line| %>
<%= project_line.user.name %>
<%= project_line.description %>
<%= number_to_currency project_line.amount, precision: 0, unit: "USD ", separator: ",", delimiter: "," %>
<%= project_line.task_status_name %>
<% end %>
Tasks are created on project form using cocoon.
In the view above, after status name, I would like to have a button for each project line that changes the status.
The solution I found so far is to create a form.
<%= semantic_form_for #project_line do |f| %>
<%= f.hidden_field :status, :value=>2 %>
<%= f.submit 'ACCEPT', class: 'btn btn-primary btn-md' %>
<% end %>
I tried to place it on a partial on projects and call it on the loop <% #project.project_lines.each do |project_line| %>but i can't get it work.
How can I get a button for each project line to change project line status?

If i understand your request, somthing along the lines of
view
<% #project.project_lines.each do |project_line| %>
<%= project_line.user.name %>
<%= project_line.description %>
<%= number_to_currency project_line.amount, precision: 0, unit: "USD ", separator: ",", delimiter: "," %>
<%= project_line.task_status_name %>
<button class="accept_to_server">Accept</button>
<% end %>
javascript(jquery)
$.('.accept_to_server').on('click', function(){
$.ajax({
url: 'path/to/accept/route'
method: 'PUT' // or what method your route uses
data: {data:you, need:here}
}).done(function(data){
// update your view
}).fail(function(){
// something went wrong
});
};
Hope this helps out.

Related

checkbox in rails view updating database value without Javascript

I am trying to make the following:
Any solution to update the database with the value and with the checkbox without using JavaScript with a click on or click off?
<%= form_for :act_as do |f| %>
<%= f.check_box(:suspend_flag, { checked: (ActAs.find_by(ModifiedID:#misc_data[:user]).SuspendFlag ? false :true), class: 'abcd' }, 0, 1) %>
<%= f.submit %>
<% end %>

Passing Rails 5 Form Value to Controller as Paramerter

I am having trouble passing a non-model form field from view to controller. The value I am trying to pass is amount
Thank you.
views/donations/index.html.erb
<%= form_tag donations_path, style: (current_user.card_last4? ? "display:none" : nil) do %>
<div id="error_explanation">
<% if flash[:error].present? %>
<p><%= flash[:error] %></p>
<% end %>
</div>
<article>
<%= label_tag(:amount, 'Donation Amount:') %>
<%= text_field_tag(:amount) %>
</article>
<%= link_to 'Donate', new_donation_path(amount: :amount), class: 'btn btn-primary', id: 'donateButton' %>
<% end %>
controllers/donations_controller.erb
def create
customer = current_user.stripe_customer
amount = params[:amount]
token = params[:stripeToken]
begin
Charge.charge(amount, token)
end
...
models/charge.rb
def self.charge(amount, token)
charge = Stripe::Charge.create(
amount: amount,
source: token,
currency: 'usd',
description: 'Test Charge'
)
end
...
Use a view tag like
<%= text_field_tag 'donation[amount]' %>
And permit the parameter in your controller
def donation_params
params.require(:donation).permit(:amount)
You can access the value with donation_params[:amount].
You shouldn't use link_to to trigger the form submission. Use submit_tag instead. Besides that, make sure your strong params whitelist whatever you're submitting.
I guess you are not using strong params. Why not just add |f| at the end like this
<%= form_tag donations_path, style: (current_user.card_last4? ? "display:none" : nil) do |f| %>
and then use <%= f.text_field :amount %>
Then at the params you should do something like params["donation"]["amount"] to get the value
EDIT: at the end change link_to for f.submit

Setting default value for Rails select helper block

How can I set default value in Rails select helper block?
<div class="field">
<label>Gender</label>
<%= f.select :gender, [], { prompt: 'Select gender', selected: 'Female' }, { :class => 'ui selection dropdown' } do %>
<% Subject.genders.keys.each do |c| %>
<%= content_tag(:option, value: c, class: 'item') do %>
<%= content_tag(:i, '', class: "#{c.downcase} icon") %>
<%= content_tag(:span, c) %>
<% end %>
<% end %>
<% end %>
</div>
I tried setting it with :selected option but it doesn't work.
I don't think if selected exist for f.select.
You can use options_for_select(values, value_selected)
Suggestion:
You can create a file named app/helpers/select_helper.rb. In this file, you create a function like this:
def subject_genders_values
Subject.genders.each do |c|
[c.value, c.value]
end
end
your function subject_genders_values can be re-used. And every time if you want a select box, you can create your function in this file.
Notice: add
include SelectHelper
in application_helper.rb
And your views:
<%= f.select :gender,options_for_select(subject_genders_values, 'Female') %>
The option Female will be selected if it's a part of the list.

how to link form_tag to button rails 4

I have a selection box on my page, and when I click the submit button I want to take the selection choice to the server as either a post or get variable (I don't think it matters). How do I link this form:
<%= form_tag(store_rates_path, method: 'get') %>
<%= label_tag(:year, "From (year)") %>
<%= select_tag(:year, options_for_select(get_select_options(1980, 2014))) %>
to this button:
<%= button_tag(link_to("Get Rates", store_rates_path))%>
You only need to provide the path to the form_for method, to link it to the rates action of your stores controller:
<%= form_tag(store_rates_path, method: "get") do %>
<%= label_tag(:year, "From (year)") %>
<%= select_tag(:year, options_for_select((1980..2014).to_a)) %>
<%= button_tag "Get Rates" %>
<% end %>
In your rates action you can then retrieve the :year parameter passed as follows:
def rates
#year = params[:year]
end
You also need to define the route in your routes.rb file as follows, if you haven't yet:
get 'stores/rate', to: 'stores#rate', as: 'store_rates'
IMPORTANT
Just note that if the rates belong to a specific store, meaning the url is something like stores/1/rate then the above get must be stores/:id/rate, which also means you need to pass the store.id to the store_rates_path in your form: store_rates_path(#store)
You can use rails submit_tag helper
<%= form_tag(store_rates_path, method: 'get') %>
<%= label_tag(:year, "From (year)") %>
<%= select_tag(:year, options_for_select(get_select_options(1980, 2014))) %>
<%= submit_tag "Get Rates" %>
<% end %>
OR
If you want to use a link or button to submit your form parameters then you can use some js magic to achieve it:
<%= form_tag store_rates_path, id: "store-form", method: 'get' %>
<%= label_tag(:year, "From (year)") %>
<%= select_tag(:year, options_for_select(get_select_options(1980, 2014))) %>
<%= link_to "Get Rates", "#", id: "store-form-btn" %>
<% end %>
$(document).on("click","#store-form-btn",function(e){
e.preventDefault();
$("#store-form").submit();
});

twitter-bootstrap-rails nav_bar helper method

hy i'm trying to use this gem to generate a responsive nar_bar.
it work fine for me:
<%= nav_bar fixed: :top , brand: "Some_brand" , :responsive => true do %>
<%= menu_group pull: :right do %>
<%= drop_down "Services" do %>
<%= menu_item "a",a_path %>
<%= menu_item "b",b_path %>
<%= drop_down_divider %>
<%= menu_item "c",c_path %>
<% end %>
<%= menu_item "About",about_path %>
<%= menu_item "Portfolio",portfolio_path %>
<% end %>
now i want to use a logo.jpg image in brand instead "Some brand". Something like this:
<%= nav_bar fixed: :top , brand: :image_tag('logo.jpg') , :responsive => true do %>
i don't know if the helper method work whit image_tag. Can you help me?
I'm not entirely sure if this will work or not, but the code you showed definitely has an issue. Including the : before image_tag is telling Rails that image_tag is a symbol and not a method. You might try removing the extra colon.
<%= nav_bar fixed: :top , brand: image_tag('logo.jpg') , :responsive => true do %>

Resources