I am building an application where a user can find an event and buy a ticket to that event. Here are my files:
tickets/new.html.erb:
<%= form_tag('/ticket/finish') do %>
<% #event.ticket_types.each do |ticket_type| %>
<tr>
<td><%= ticket_type.name %></td>
<td> <%= number_to_currency(ticket_type.price, unit: 'VND ', precision: 0) %></td>
<td> <%= select_tag(:quantity, options_for_select(0..10)) %></td>
</tr>
<br>
<% end %>
<p>
<%= submit_tag 'Purchase Ticket', class: 'btn btn-primary' %>
</p>
<% end %>
tickets_controller.rb:
class TicketsController < ApplicationController
def new
#event = Event.find(params[:event_id])
#ticket = Ticket.new
end
def purchase
end
end
event.rb:
class Event < ActiveRecord::Base
belongs_to :user
belongs_to :venue
belongs_to :category
has_many :ticket_types
validates_associated :ticket_types
validates_presence_of :user
validates_presence_of :name, :extended_html_description, :hero_image_url
# validates_presence_of :venue, :category
validates_uniqueness_of :name,scope: [:venue_id, :starts_at]
def has_ticket_types?
!!self.ticket_types.present?
end
end
ticket_types.rb:
class TicketType < ActiveRecord::Base
belongs_to :event
validates_presence_of :name, :price, :max_quantity
validates_uniqueness_of :name, scope: :event_id
end
routes.rb:
Rails.application.routes.draw do
devise_for :users
root 'events#index'
get '/yourevents', to: 'events#user_events'
resources :events do
resources :tickets
end
get '/ticket/purchase', to: 'tickets#purchase'
get 'ticket/finish', to: 'tickets#finish'
resources :events do
resources :ticket_types
end
resources :venues
end
My goal is I want when user click the 'purchase ticket' button, it will lead them to a page that shows them the ticket types, and the quantity of those ticket types they have just bought. However, I have no idea how can I pass those params in my purchase action since I don't have a ticket model. Could anybody here give me some hint or guide me to the right direction? Thank you in advance.
Related
I'm trying to associate a relation betwen 1 patient with a consultation, but I'm getting a error:
Association :patient not found
In the model I have:
class Patient < ActiveRecord::Base
belongs_to :user
has_many :consultums, through: :patients
end
class Consultum < ActiveRecord::Base
belongs_to :patients
belongs_to :user
has_one :recetum
end
in the controller I have:
class ConsultationController < ApplicationController
before_action :authenticate_user!
before_action :set_consultum, only: [:show, :edit, :update, :destroy]
respond_to :html
def index
#consultation = Consultum.all
respond_with(#consultation)
end
def show
respond_with(#consultum)
end
## when I try to create a new consultation, throw me the error ##
def new
#consultum = Consultum.new
# patient_id
respond_with(#consultum)
end
def edit
end
def create
#consultum = Consultum.new(consultum_params)
#consultum.save
respond_with(#consultum)
end
def update
#consultum.update(consultum_params)
respond_with(#consultum)
end
def destroy
#consultum.destroy
respond_with(#consultum)
end
# def patient_id
# patient = Patient.find(params[:id])
# # patient = #patient.id
# #consultum.patient_id = patient
# end
private
def set_consultum
#consultum = Consultum.find(params[:id])
end
def consultum_params
params.require(:consultum).permit(:Reason_Consultation, :Diagnostic, :TX, :Labs, :Observations, :patient_id)
end
end
so, as you can see I create the function patient_id, and I'm trying to retrieve the id from the 'patient' and put into patient_id in 'consultum' table, but seems not work...
if I uncomment patient_id function throw me this error:
Couldn't find Patient without an ID
I'm stuck, any idea?
Thanks in advance
EDIT
in my view I have:
consultation/new.html.erb
<div class="page-header">
<%= link_to consultation_path, class: 'btn btn-default' do %>
<span class="glyphicon glyphicon-list-alt"></span>
Back
<% end %>
<h1>New consultum</h1>
</div>
<%= render 'form' %>
_form.html.erb
<%= simple_form_for(#consultum) do |f| %>
<%= f.error_notification %>
<div class="form-inputs">
<%= f.input :Reason_Consultation %>
<%= f.input :Diagnostic %>
<%= f.input :TX, placeholder: "optional" %>
<%= f.input :Labs, placeholder: "optional" %>
<%= f.input :Observations, placeholder: "optional" %>
<%= f.association :patient %>
</div>
<div class="form-actions">
<%= f.button :submit, "Save Consultation" %>
</div>
<% end %>
When creating associations in rails pay super close attention to the pluralization!
class Patient < ActiveRecord::Base
belongs_to :user
has_many :consultums, through: :patients
end
class Consultum < ActiveRecord::Base
belongs_to :patients
belongs_to :user
has_one :recetum
end
You will notice that your Consultum class does not have a patient relation. It has belongs_to :patients.
class Consultum < ActiveRecord::Base
belongs_to :patient
belongs_to :user
has_one :recetum
end
Also your attribute naming should follow the Ruby conventions!
Ruby uses snake_case for naming attributes and methods and will automatically treat any identifier which begins with an UPPERCASE letter as a constant!
This is not just a stylistic issue - MRI Ruby will let you reassign constants with a warning. Other versions or future versions may be less lenient.
Bad Good
.Reason_Consultation .reason_consultation
.Diagnostic .diagnostic
.TX .tx
.Lab .lab
.Observations .observations
https://github.com/bbatsov/ruby-style-guide
To call an action you can pass parameters as follow:
link_to "New Consultum", new_consultum_path(:id => here, yo have to put the paciente id)
Hope this helps!
Hello I am having a hard time debugging my code. What I need to do is create a page where the user will type in a last name and the system will return information related to it. I am stuck and can not debug it any help or guidance would be greatly appreciated
Model of my Customer table
class Customer < ActiveRecord::Base
has_many :booking
has_many :room, :through => :booking
def mail_list
"#{first} - #{last} - #{maillist}"
end
end
Here is the page that should display the data after the user enters a last name
<center><table width = 65% border = 1>
<tr> <th> Customer Name</th><th> Room Number </th> <th> Cost </th></tr>
<% #customers_list.each do |m| %>
<% p = m.cost %>
<tr> <td> <%= m.name %> </td> <td> <%= m.roomnumber %> </td> <td> <%= m.cost %> </td> </tr>
<% end %> </table> </center><br /> <br />
Here is the page that takes in the last name
<%= form_tag(bookin_bookout_path, :controller => "bookin", :action => "bookout", :method => "post") do %>
<div class="field">
<%= label_tag :Customer_Name %><br />
<%= text_field_tag :customer_name_in %>
</div>
<div class="actions">
<%= submit_tag "Submit Customer Name" %>
</div>
<% end %>
Model of my rates table
class Rate < ActiveRecord::Base
validates :season, :room, :cost, presence: true
has_many :booking
has_many :room, :through => :booking
end
Here is the Controller of my bookin that runs the user putting in last name and posting results
class BookinController < ApplicationController
def bookin
def bookout
#customer_name = params[:customer_name_in]
r = Customer.find_by_last(#customer_name)
#room_list = r.rooms
end
end
end
Here is the Model of my room table
class Room < ActiveRecord::Base
has_many :booking
has_many :customer, :through => :booking
end
Here is the Model of my booking table
belongs_to :room
belongs_to :customer
belongs_to :rate
end
Here is my routes
Hotel::Application.routes.draw do
resources :bookings
get "bookin/bookout"
post "bookin/bookout"
get "listin/listout"
resources :customers
get "manin/manout"
resources :users
get "mailin/mailout"
get "showin/showout"
resources :roomins
get "ratein/rateout"
get "foodin/foodout"
resources :apples
resources :rooms
resources :menus
resources :rates
get "starter/home"
resources :foods
get "bookin/bookname"
post "bookin/bookname"
root 'starter#home'
end
You need to use pluralized model name with has_many, use this
has_many :bookings
has_many :rooms, :through => :bookings
You have not followed rails' convention over configuration in naming your models and controllers
im trying to build my 3rd model(result) along with 2nd model ( generator). I call the result form from my Generator show.html.erb and when i clicked the submit button in the form i get this error param not found: generator
routes.rb
resources :users do
resources :generators
resources :results
end
Generator show.html.erb
...
<legend><strong>Binding Time Analysis</strong></legend>
<%= render "results/form" %>
</fieldset>
<% end %>
Result.controller
def new
#result=Result.new
end
# GET /results/1/edit
# POST /results
# POST /results.json TGATGAACATCATGATGAGGTGATGACATCACATCATTGACTGATGCATCATGATG
def create
#result = #generator.build_result(result_params)
#result=#result.generate_result(result_params)
#generator.result.save
redirect_to user_generators_path
end
def result_params
params.require(:result).permit(:ncbi_ref_seq,:genome_seq,:genome_sample,:binding_times,:amp_frags,:seqpos1,:seqpos2)
end
User.rb
class User < ActiveRecord::Base
has_many :generators
has_many :results, :through=>:generators
Generator.rb
class Generator < ActiveRecord::Base
has_one :result , :dependent => :destroy
belongs_to :user
attr_accessible :choice, :primer_length, :random_primer_generated, :generator_id
Result.rb
class Result < ActiveRecord::Base
attr_accessible :generator_id,:ncbi_ref_seq,:genome_seq, :genome_sample
belongs_to :generator
Form
<%= form_for(#generator.build_result,:url =>user_generator_path(:user_id => current_user.id, :id => #generator.id),:html =>{:method=>:put}) do |f| %>
<%= text_field_tag(:ncbi_ref_seq ,nil, placeholder: 'Accession Number')%>
</p>
<p id="highlight"><font size ="5">
OR
</font></p>
<p id="FASTA">
<strong><font size ="3">Paste your sequence here: ( FASTA format ) :</font></strong>
<%= text_area_tag(:genome_seq,nil,size: "50x10",placeholder: 'Input your sequence here')%>
</p>
<input type="reset" value="Reset" id="reset">
<%= submit_tag 'Analyze' %>
<% end %>
Generator.controller generator_params
def generator_params
params.require(:generator).permit(:generator_id,:primer_length,:choice,:random_primer_generated,:no_A,:no_T,:no_G,:no_C,:user_seq)
end
I'm creating review to my posts, all works but i dont know how to show who wrote the review
i'm trying this:
class User < ActiveRecord::Base
has_many :posts
has_many :reviewers, :class_name => 'Post', :foreign_key => 'reviewer_id'
end
class Post < ActiveRecord::Base
belongs_to :user
belongs_to :category
has_many :reviews
belongs_to :reviewers, class_name: 'User', :foreign_key => 'reviewer_id'
default_scope -> { order('created_at DESC') }
end
class Review < ActiveRecord::Base
belongs_to :post
belongs_to :user
end
class ReviewsController < ApplicationController
def new
#post = Post.find(params[:post_id])
#review = #post.reviews.new(post_id:params[:post_id])
end
def create
#post = Post.find(params[:post_id])
#review = #post.reviews.build(review_params)
if #review.save
flash[:success] = "Ваш отзыв добавлен"
redirect_to post_path #post
else
render 'new'
end
end
private
def review_params
params.require(:review).permit(:post_id, :body, :reviewer_id).merge(:reviewer_id => current_user.id)
end
end
and my view
<% #post.reviews.each do |review| %>
<p>
<strong>reviewer:</strong>
<%= review.reviewer.email %>
</p>
<p>
<strong>review:</strong>
<%= review.body %>
</p>
<% end %>
my migration
class CreateReviews < ActiveRecord::Migration
def change
create_table :reviews do |t|
t.text :body
t.references :post, index: true
t.references :reviewer, index: true
t.timestamps
end
end
end
but rails given error undefined method `reviewer' for #
Help please dsfsdf
I think that you have a pluralization issue:
A post have many reviews by many reviewers (one for each review). But you are storing the foreign key within the post so you written the problematic line:
belongs_to :reviewers, class_name: 'User', :foreign_key => 'reviewer_id'
The issue here is that it is a singular association with a plural name.
I think that you are trying to say here that a
class Post
have_many :reviewers, class_name: 'User', through: :reviews
end
But as you are trying to access the reviewers from the review what you really need is to add:
class Review
belongs_to :reviewer, class_name: 'User'
end
Then you can access the reviewers from the review as expected:
<% #post.reviews.each do |review| %>
<p>
<strong>reviewer:</strong>
<%= review.reviewer.email %>
</p>
<p>
<strong>review:</strong>
<%= review.body %>
</p>
<% end %>
There is also an error in User:
has_many :reviewers, :class_name => 'Post', :foreign_key => 'reviewer_id'
As it should be:
has_many :reviews, :foreign_key => 'reviewer_id'
You need to be using .user. Check the belongs_to in your model.
Review.first.user
As a previous poster pointed out, your user association for Review is :user, so your view should probably look like this:
<% #post.reviews.each do |review| %>
<p>
<strong>reviewer:</strong>
<%= review.user.name unless review.user.nil? %>
</p>
<p>
<strong>review:</strong>
<%= review.body %>
</p>
<% end %>
I want to create a multiple form for editing scores from a different model.
The main model is a Formrule model that consists of a habtm association with a Scoretype model
and has a habtm association with a Room model.
Both models are used to query a Scores model resulting in a #scores instance. It is for this instance I want to create a form, but the problem is that no field_for are being created. I know that the #scores is populated correctly, but the form does not show up.
This is the form as I have it now
<%= form_tag '/scores/update_scores' do %>
<table>
<tr>...</tr>
<% for score in #scores %>
<% fields_for :scores, score do |score| %>
<tr>
<td>
<%= score.hidden_field(:form_id) %>
<%= score.hidden_field(:team_id) %>
<%= score.hidden_field(:scoretype_id) %>
</td>
<td>
<%= score.number_field :scorevalue %>
</td>
</tr>
<% end %>
<% end %>
</table>
<%= submit_tag 'Update' %>
<% end %>
And these are the Models:
Formrule
class Formrule < ActiveRecord::Base
belongs_to :form
has_and_belongs_to_many :scoretypes
has_and_belongs_to_many :rooms
has_many :teams, :through => :rooms
end
Scoretype
class Scoretype < ActiveRecord::Base
has_many :scores
has_and_belongs_to_many :formrules
end
Room
class Room < ActiveRecord::Base
has_many :teams
has_and_belongs_to_many :formrules
end
Team
class Team < ActiveRecord::Base
has_many :scores
belongs_to :room
belongs_to :group
end
Score
class Score < ActiveRecord::Base
belongs_to :form
belongs_to :team
belongs_to :scoretype
validates_uniqueness_of :id, :scope => [:team, :scoretype]
end
And finally, the used controller (Formrule)
def show
#formrule = Formrule.find(params[:id])
#scoretypes = #formrule.scoretypes.all.collect
#rooms = #formrule.rooms.all.collect
#teams = Team.find(:all, :conditions => {:room_id => #rooms})
#scores = Score.order("team_id").all(:conditions => {:scoretype_id => #scoretypes, :team_id => #teams})
...
end
Why is the form not showing up? any suggestions?
Thank you all in advance!
Try using <%= fields_for ... %> instead of <% fields_for ...%>.