Pass parameter from view to controller - ruby-on-rails

I have a next code in my view:
<% #todos.each do |todo| %>
<tr>
<td><%= todo.description %></td>
<td><%= link_to 'Create action', new_simple_action_path } %></td>
I want to pass todo.description to SimpleAction-controller. I can rewrite my code like this and it will be work:
<td><%= link_to 'Create action', { :controller => 'simple_actions', :action => 'new', :todo => todo.description } %></td>
But I'm just wondering if is it possible to pass variable as parameter using new_simple_action_path ?

This should work:
link_to 'Create action', new_simple_action_path(:todo => todo.description)

Related

Configure Sunspot Solr search with scope

Controller
class GearsController < ApplicationController
before_action :set_gear, only: [:show, :edit, :update, :destroy]
def primary
#gears = Gear.search do
with(:Lab, 'Primary')
order_by(:RU, :desc)
end
end
Model
class Gear < ActiveRecord::Base
validates_uniqueness_of :Nic1mac, :Nic2mac, :Nic3mac, :IBmac, :SN, :allow_blank => true
attr_readonly :Devtype, :Nic1mac, :Nic2mac, :Nic3mac, :IBmac, :SN
scope :Mini, -> { where(lab: 'Mini') }
scope :Primary, -> { where(lab: 'Primary') }
scope :Support, -> { where(lab: 'Support') }
scope :M5, -> { where(lab: 'M5') }
scope :P5, -> { where(lab: 'P5') }
searchable do
integer :RU
text :Devname
text :Devtype
string :Lab
text :Swportib
text :Swport1
text :Swport2
text :Swport3
text :IBip
text :Nic1
text :Nic2
text :Nic3
text :Nic1mac
text :Nic2mac
text :Nic3mac
text :IBmac
text :Psu1
text :Psu2
text :SN
end
end
View
<% provide(:title, "SP4 Primary") %>
<p id="notice"><%= notice %></p>
<h1>SP4 Primary</h1>
<%= form_tag gears_path, :method => :get do %>
<p>
<%= text_field_tag :search, params[:search] %>
<%= submit_tag "Search", :name => nil %>
</p>
<% end %>
<head>
<style>
table, th, td { border: 1px solid black;}
th, td { padding: 15px;}
</style>
</head>
<table>
<thead>
<tr>
<th>RU</th>
<th>Dev Name</th>
<th>Dev Type</th>
<th>Lab</th>
<th>SW PortIB</th>
<th>SW Port1</th>
<th>SW Port2</th>
<th>SW Port3</th>
<th>IB IP</th>
<th>NIC1</th>
<th>NIC2</th>
<th>NIC3</th>
<th>NIC1 Mac</th>
<th>NIC2 Mac</th>
<th>NIC3 Mac</th>
<th>IB Mac</th>
<th>PSU1</th>
<th>PSU2</th>
<th>SN</th>
<th colspan="3"></th>
</tr>
</thead>
<tbody>
<% #gears.each do |gear| %>
<tr>
<td><%= gear.RU %></td>
<td><%= gear.Devname %></td>
<td><%= gear.Devtype %></td>
<td><%= gear.Lab %></td>
<td><%= gear.Swportib %></td>
<td><%= gear.Swport1 %></td>
<td><%= gear.Swport2 %></td>
<td><%= gear.Swport3 %></td>
<td><%= gear.IBip %></td>
<td><%= gear.Nic1 %></td>
<td><%= gear.Nic2 %></td>
<td><%= gear.Nic3 %></td>
<td><%= gear.Nic1mac %></td>
<td><%= gear.Nic2mac %></td>
<td><%= gear.Nic3mac %></td>
<td><%= gear.IBmac %></td>
<td><%= gear.Psu1 %></td>
<td><%= gear.Psu2 %></td>
<td><%= gear.SN %></td>
<td><%= link_to 'Show', gear %></td>
<td><%= link_to 'Edit', edit_gear_path(gear) %></td>
<td><%= link_to 'Destroy', gear, method: :delete, data: { confirm: 'Are you sure?' } %></td>
</tr>
<% end %>
</tbody>
</table>
<br>
Above is my Controller and Model code. I am trying to set up the search so it only searches through records within the scope specified. Currently it will return all results and does not pay attention to the scope. I believe I am close to making it work but just don't have the right syntax. Then again I may be wrong, thus any help suggestions or different ways of doing this are welcome.
You can add a code like this in your search method
with(:lab, 'Support')
and in your model add this
string :lab
The solution was adding without in my gears_controller.rb file.
gears_controller.rb
def rack1
#gears = Gear.where("Lab like ? OR Lab like ? OR Lab like ?", "%Primary%", "%Mini%", "%Support%")
#search = Gear.search do
fulltext params[:search]
without(:Lab, "P5")
without(:Lab, "M5")
order_by(:RU, :desc)
end
#gears = #search.results
end
Adding the without fields allowed me to only search through the records with the specific Lab values that were specified in my #gears variable.

Stripe - Creating a new card for customer

I am trying to implement the ability for a customer to create a new card.
I have created a form in which the customer creates a new card:
<table class="table">
<%= simple_form_for(#user, :url => billing_path, html: {class: 'form-horizontal' }) do |f| %>
<tr>
<td>Country</td>
<td><%= f.text_field :saddress_country, value: card1.address_country %> </td>
</tr>
<tr>
<td>Billing Address</td>
<td><%= f.text_field :sbilling_address1, value: card1.address_line1 %> </td>
<td><%= f.text_field :sbilling_address2, value: card1.address_line2 %> </td>
</tr>
<tr>
<td>City / State / Zip</td>
<td><%= f.text_field :saddress_city, value: card1.address_city %> </td>
<td><%= f.text_field :saddress_state, value: card1.address_state %> </td>
<td><%= f.text_field :saddress_zip, value: card1.address_zip %> </td>
</tr>
<tr>
<td>Credit Card Number </td>
<td><%= f.text_field :scardnumber, placeholder: card1.last4, value: cardnumberempty %></td>
<td><%= f.text_field :scvc, placeholder: "CVC", value: cvcempty %></td>
</tr>
<tr>
<td>Card's Info</td>
<td><%= f.text_field :sname, value: card1.name %> </td>
<td><%= f.text_field :sexp_month, value: card1.exp_month %> </td>
<td><%= f.text_field :sexp_year, value: card1.exp_year %> </td>
</tr>
<tr><td><%= f.submit %></td></tr>
<% end %>
</table>
And then in the controller I grab the information and create the new card:
def create_card_stripe
#user = current_user
customer = Stripe::Customer.retrieve(current_user.stripe_customer_token)
customer.cards.create({
:card => current_user.stripe_card_token,
:number => params[:user][:scardnumber],
:exp_month => params[:user][:sexp_month],
:exp_year => params[:user][:sexp_year],
:cvc => params[:user][:scvc],
:name => params[:user][:sname],
:address_line1 => params[:user][:saddress_line1],
:address_line2 => params[:user][:saddress_line2],
:address_city => params[:user][:saddress_city],
:address_zip => params[:user][:saddress_zip],
:address_state => params[:user][:saddress_state],
:address_country => params[:user][:saddress_country]
})
#user.update_attribute(:stripe_card_id, customer.active_card.id)
if customer.save
flash.alert = "Billing information updated successfully!"
redirect_to edit_user_registration_path
else
flash.alert = "Stripe error"
redirect_to edit_user_registration_path
end
end
When I submit the form I get the following error that comes from Stripe:
"Received unknown parameters: number, exp_month, exp_year, cvc, name, address_city, address_zip, address_state, address_country".
So what it says is that I am sending wrong parameters. But if you check here those parameters are there (Child parameters).
Any clue on what I am doing wrong? Any guidance at all on how to create a new card using the Stripe API will be really helpful. Thank you.
There is two ways of adding a card using Stripe.
1) Using the card token. If you have a token, like you do, you don't need anything else, so you can just do
customer.cards.create({
:card => current_user.stripe_card_token
})
2) You could also do this:
customer.cards.create({
:card => {
:number => params[:user][:scardnumber],
:exp_month => params[:user][:sexp_month],
:exp_year => params[:user][:sexp_year],
:cvc => params[:user][:scvc],
:name => params[:user][:sname],
:address_line1 => params[:user][:saddress_line1],
:address_line2 => params[:user][:saddress_line2],
:address_city => params[:user][:saddress_city],
:address_zip => params[:user][:saddress_zip],
:address_state => params[:user][:saddress_state],
:address_country => params[:user][:saddress_country]
}
})
So as you can see here, the important thing is that the details are nested into the card attribute.
Evidently, you've done a mix of the two, you are best off choosing one (token is better) and sticking with it. Is that clear?

Pagination not working correctly in rails

I am trying to use will_paginate for a list of items I want to display.
There is a form with drop down selections to fetch objects by status.
This is my controller
def list
#person = Person.find_by_id(session[:person_id])
params[:status] = params[:redirect_status] if params.has_key?('redirect_status')
#statuses = Peptide.statuses
#status = 'Not Ordered'
#status = params[:status] if params[:status] != nil || params[:status] == ''
#peptides = Peptide.find(:all, :conditions => ['status = ?', #status]).paginate(:per_page => 30, :page => params[:page])
if Peptide.count.zero?
flash[:notice] = "There are not any peptides entered"
redirect_to :action => 'new'
end#if zero
end
This is in the view
<form action="/peptide/create_spreadsheet_of_not_ordered" enctype="multipart/form-data" method="post">
<table class="sortable" cellpading="5" cellspacing="2" width="100">
<tr class= "header-line">
<th>SS</th>
<th>Status</th>
<th>Peptide</th>
<th>Gene</th>
<th>Submitter</th>
<th>Created</th>
<th>Updated</th>
<th>Sequence</th>
<th>Modification</th>
<th>Vendor</th>
</tr>
<% for #peptide in #peptides.reverse %>
<tr valign = "top" class= "<%= cycle('color_one', 'color_two') %>">
<!--an error here during development likely indicates that the people table has not be repopulated or
that no submitter primer is present for a primer-->
<td sorttable_customkey = 0 > <%=check_box_tag "box[]", #peptide.id %>
<td><%= #peptide.status%></td>
<td class><%= link_to #peptide.name, :action => :report, :id => #peptide.id %></td>
<td><%= gene_links(#peptide.genes) rescue 'Error'%></td>
<td><%= #peptide.submitter.name rescue "" %></td>
<td <%= sorttable_customkey_from_date(#peptide.created_at)%> >
<%= #peptide.created_at.strftime("%b %d %Y") rescue "Unknown"%>
</td>
<td <%= sorttable_customkey_from_date(#peptide.updated_at)%> >
<%= #peptide.updated_at.strftime("%b %d %Y") rescue "Unknown"%>
</td>
<td><%= #peptide.sequence%></td>
<td><%= #peptide.modifications%></td>
<td><%= #peptide.vendor%></td>
<%= buttons() %>
</tr>
<%end%>
<%= will_paginate #peptides %>
</table>
<br>
<%= submit_tag "Create Spreadsheet"%>
The default list is grouped by status ordered.
however when I select any other status and submit the form the pagination links take me back to the default status.
Please help me resolve this issue.
Thanks
Without seeing the view, it sounds like you need to add the current params as an argument to the links for the other statuses...
Update
Try adding the params to your status link:
<%= link_to #peptide.name, peptide_path(#peptide, params), :action => :report, :id => #peptide.id %>
The documentation is here.

Rails view conditionals if/elsif on user fields

I'm trying to show a table of user data on my site - so I'm using a loop that iterates over the users and displays their info. Before, it was really simple, but I wanted to add conditionals that handled cases where they might not have a city, state, or either entered. When I only have one case entered, it works fine. When I have both, it works fine. User location is defined in the model as a JOIN of city and state. So don't worry there. But when I have neither, it doesn't display the '---' that I want. What am I missing here?
<tr>
<% #user.each do |user| %>
<td><%= link_to user.username, public_profile_path(user.username) %><br/></td>
<% if user.state != nil and user.city != nil %>
<td><%= user.location %></td>
<% elsif user.state != nil and user.city == nil %>
<td><%= user.state %></td>
<% elsif user.state == nil and user.city != nil %>
<td><%= user.city %></td>
<% else %>
<td>---</td>
<% end %>
<% if user.phone != nil %>
<td><%= user.phone %></td>
<% else %>
<td>-</td>
<% end %>
<td><%= mail_to user.email.to_s, "Email this User", :cc => "michaelomchenry#gmail.com",
:subject => "Contact from Overflow Member" %></td>
</tr>
<% end %>
I'd add a helper method, called location_display or something along those lines that handled all the logic around what should be displayed.
def location_display(user)
return user.location if user.state && user.city
return user.state if user.state && user.city.blank?
return user.city if user.city && user.state.blank?
"----"
end
Then, in your view, just replace all those lines with:
<td><%= location_display(user) %></td>

Setting a params hash to a variable within the controller (rails)

I'm very new to rails and have this fairly basic question:
I have a form that takes in an array:
<td><%= fields_for "days" do |form| %>
M: <%= form.check_box "", {}, 'M', '' %>
T: <%= form.check_box "", {}, 'T', '' %>
W: <%= form.check_box "", {}, 'W', '' %>
Th: <%= form.check_box "", {}, 'Th', '' %>
F: <%= form.check_box "", {}, 'F', '' %>
<% end %>
This should be accessible through params[:days]. How can I assign params[:days] to a variable within the controller? #days is not correct here, right? (There's no Days object, so there's no instance of that object). What should go in the [correct_variable] slot below?
[correct_variable] = params[:days]
Thanks!
In response to comments:
I tried using #days, but for some reason it wouldn't get called where I'd like it to. In particular, I'd like to pass it to my Search model:
class Search < ActiveRecord::Base
attr_accessible :name, :day, :units, :instructor
def courses
#courses ||= find_courses
end
private
def find_courses
Course.find(:all, :conditions => conditions)
end
def day_conditions
["courses.day LIKE ?", "%"+ #days.join("%")+"%"]
end
I first instantiate #days in my courses controller (which is connected, through the index method, to a partial that uses an instance of the Search object.
More code:
From courses/index.html.erb:
<% if #search.save %>
<div id = "results_scroll">
<%= render :partial => 'searches/search_results' %>
</div>
<% else %>
From search_results partial:
<% "Search" %>
<table>
<tr>
<th align="left" width="25%"><%= 'Name' %></th>
<th align="left" width="10%"><%= 'Number' %></th>
<th align="left" width="20%"><%= 'Instructor' %></th>
<th align="left" width="10%"><%= 'Room' %></th>
<th align="left" width="5%"><%= 'Day' %></th>
<th align="left" width="5%"><%= 'Units' %></th>
<th align="left" width="15%"><%= 'Time' %></th>
<th align="left" width="10%"><%= 'Limitations' %></th>
</tr>
<%= render :partial => #search_show.courses %>
</table>
From the courses controller (this has the #search_show variable).
def index
#courses = Course.order(sort_column + " " + sort_direction)
#search = Search.new
#search_show = Search.last
#title = "List"
#days = params[:days]
Finally, the _course.html.erb partial:
<tr>
<td><%= course.name %></td>
<td><%= course.number %></td>
<td><%= course.instructor %></td>
<td><%= course.room %></td>
<td><%= course.day %></td>
<td><%= course.units %></td>
<td><%= course.time %></td>
<td><%= course.limitations %></td>
<td><%= link_to 'Show', course %></td>
</tr>
This is not really an answer, but there is some problems with how you are trying to access your #days variable you set in the controller.
# your controller
...
def index
...
#days = params[:days]
# your model
...
private
def day_conditions
["courses.day LIKE ?", "%"+ #days.join("%")+"%"]
end
The #days, in your model, you are calling is not going to access the #days in your controller. Each #days belongs to their respective instances.
You should find a method of sending the #days from your controller into your models, instead of using global variables.
Solved the problem by using a $days global variable instead of #days.

Resources