I have a quiz application that uses acts_as_taggable and will_paginate to show one item at a time.
I created a model called MultipleChoiceQuestion (MCQ). Each MCQ has associated things with it, along with tags.
What I want to do is create a variety of different custom methods in the controller that will only collect questions that have certain tags attributed to the MCQ.
I want to then let the user see one question at a time with the custom method defined tags.
So, for example:
The controller:
def pharmas1
#sem1_pharma_mcq = MultipleChoiceQuestion.tagged_with(["pharmacology", "sem1"], :match_all => true).all.paginate(:page => params[:page], :per_page => 1)
end
The view
<% #sem1_pharma_mcq.each do |sem1_pharma_mcq| %>
<hr>
<%= sem1_pharma_mcq.question %> <Br>
<%= sem1_pharma_mcq.answer_one %>
<%= sem1_pharma_mcq.answer_two %>
<%= sem1_pharma_mcq.answer_three %>
<%= sem1_pharma_mcq.answer_four %>
<%= sem1_pharma_mcq.answer_correct %>
<%= sem1_pharma_mcq.answer_explanation %>
<%= sem1_pharma_mcq.tag_list %>
<hr>
<%= link_to "Next Question →", #sem1_pharma_mcq.next, :class => 'button next-question' %>
<%= will_paginate #sem1_pharma_mcq %>
<% end %>
The model:
acts_as_taggable
def next
MultipleChoiceQuestion.tagged_with(["sem1", "pharmacology"], :match_all => true).order(id: :asc).limit(1).first
end
def prev
MultipleChoiceQuestion.where("id < ?", id).order(id: :desc).limit(1).first
end
The routes.rb
resources :multiple_choice_questions, path: 'mcqs' do
get 'pharmas1', :on => :collection
end
I'm trying to accomplish two things:
Allow the user to view one question at a time with the rails "show" action/method, rather than using will_paginate to show one question at a time on the index.
I don't know how to go about this, and my functionality is working only through a custom gem? How can I show custom actions once per page in the show view template?
Allow the user to go previous/next on this show action/method but making sure that only questions of a certain tag are being applied.
The error here is: undefined method `next' for # when I try load the custom method named "pharmas1" -- which is defined in the multiple_choice_questions_controller.rb file
Related
I am currently into learning Ruby on Rails and I am stuck with a pretty simple thing. I already tried to google it (or look it up here), but I could not find the right answer (due to my RoR-beginnings it might searched with the wrong terms).
I am working on a small learning project to list different items (items_controller). Each of these items belong to a category. But because I want the users to either create, update or delete a category I created a categories_controller. Both controllers and views are working fine, but I want to include the Category of each item on the index-view (which is also the root_path) of my Items Controller. Here I get stuck (with the code and the logic behind it as well). What is the best way to do this?
Somehow I managed to get the category name into the edit.html.erb-form of the items_controller, but I don't really understand how:
items_controller:
def edit
#item = Item.find(params[:id])
#categories = Category.all.map{ |c| [c.name, c.id] }
end
edit.html.erb (this is actually in _form.html.erb and rendered in edit.html.erb):
<%= simple_form_for #item, :html => { :multipart => true } do |f| %>
<%= f.select :category, #categories %>
<%= f.input :title %>
<%= f.input :url %>
<%= f.button :submit %>
<% end %>
If you want to include the category of each items, You need to specify which column of categories table you want to display. Example : a column called name:
<% #items.each do |item| %>
<%= item.category.name %>
<% end %>
I'm building a web interface to accompany a mobile app I'm building. I have a drop down select menu that lists a bunch locations.
On selection of a location I want to make a call to a method in my controller and grab some destinations within the location that was selected (each location has several destinations).
I then would like to render my show template with these results allowing the user to select a destination and make a booking.
This is what I have so far:
My view with a list of resorts:
<%= form_tag :url => { :action => :show } do %>
<%= select_tag :resort , options_for_select(#resorts), :prompt => 'Select Resort', :onchange => 'submit()' %>
<% end %>
Controller:
class HomeController < ApplicationController
def index
#resorts = ["A","B", "C", "D", "E"]
end
def new
end
def edit
end
def create
end
def show
#activities = Parse::Query.new("Activity").tap do |a|
a.eq("resort", params[:resort])
end.get
end
end
Just slightly confused. Using form_for makes more sense to me with CRUD in mind and also because the form is object based.
I'd like to just take the selected resorted and pass it into a method in my controller that goes into a database and grabs a bunch of destinations. I then want to list these destinations on my show page where a user can click and be taken to another page where they can make a booking at that destination.
My above code doesn't work. I have resources :home in my routes file.
However when I try to load my page with the form I get:
No route matches {:action=>"show", :controller=>"home"} missing required keys: [:id]
How do I pull this off?
I went on my lynda account and pulled up a rails essential tutorial which I'll have to use to refresh my memory some time tomorrow but the tutor doesn't cover use of select_tag.
Would appreciate some help here
Thanks for your time
So a few thoughts. Not sure why you are using form_tag and also not sure why you aren't using Rails idiomatic conventions.
Declare a resource in your routes for #resorts, like so:
resources :resorts
Then just use Rails form_for helper like:
<%= form_for #resorts, url: {action: "create"}, html: {class: "nifty_form"} do |f| %>
<%= f.select :resort, (insert your other options) %>
<%= f.submit "Create" %>
<% end %>
I have not tested the above code, so play around with it, but that should work.
However, let me save you some headache. Checkout SimpleForm.
For your models, you would want to setup an association between your locations and destinations.
class Location < ActiveRecord::Base
belongs_to :resort # or whatever the relation is
has_many :destinations
end
class Destination < ActiveRecord::Base
belongs_to :location # This assumes there is just a one-to-many relationship between Location and Destination
end
Make sure you have a LocationsController with all the actions.
In this case, your SimpleForm form would look something like this:
<%= simple_form_for #locations do |f| %>
<%= f.input :name %>
<%= f.association :password %>
<%= f.button :submit %>
<% end %>
That approach will make your life much easier. Take a look at the collections methods in Simple Form. Rails can make your life difficult with the built in form helpers.
Hope that helps!
In your routes, add
get '/choose_resort' => 'home#show' #you can name the get whatever
Then in your form...
<%= form_tag choose_resort_path do %>
That being said... you should have your query at a separate endpoint, and redirect to the show page. That should get you moving, methinks.
The show action needs an id of the object you are showing. Change your controller:
class HomeController < ApplicationController
def index
#resorts = [["A",1], ["B",2], ["C",3], ["D",4], ["E",5] ]
end
And your view
<%= select_tag :id , options_for_select(#resorts), :prompt => 'Select Resort', :onchange => 'submit()' %>
That gives your show action the proper resort id. You'll have to adjust that action to find the right activities relevant to the resort.
Ive got a select_tag field at the top of a page and trying to get the selected option to change the content on the page based on the users selection.
Im a learner and have found pieces of information around but without detailed examples and good explanations on how to best approach and implement.
The scenario is as follows:
I have a belongs_to association between a project and documents and in one of my views which lists documents, I want to only show all the documents that belong to the currently selected project in the select tag.
Passing the selected project's id to the documents index action which only shows documents for a specified project id via a link_to tag came to mind. This would thus refresh the page with the correct documents in the view but I believe that is not the correct way to do it and that I cant use link_to tags as options in a select_tag. Can anyone help and offer an example?
I would suggest using the form.select method and options_for_select as in
f.select :attribute, options_for_select(#array, default_value)
and in your controller you should create or update using the submitted parameter
n = record.new(:attribute => params[:attribute])
have fun
In your controller:
def index
if params[:project]
#documents = Document.where(:project => params[:project]
else
#projects = Project.all
end
end
In your form/view:
<%= form_tag 'projects', :method => :get do %>
<%= options_from_collection_for_select(#projects, :id, :name)
<%= submit_tag %>
<% end %>
<% if #documents %>
<%= #documents.each do |d| %>
....
<% end >
<% end %>
new on rails, i am having problem in passing select_tag value(in the view file) to controller.
my view controller file is like
class ProjectStatusController < ApplicationController
def index
#projects = Project.find(:all, :select => "name")
end
def show
lookup = params[:project]
#rows = Project.find_by_lookup(lookup)
end
end
and view file is like
<% form_tag("project_status", :controller => "ProjectStatus", :action => "show", :method=>'get' ) do %>
<%= select_tag 'project', options_from_collection_for_select(#projects,"id", "name"),:onchange => "this.form.submit();" %>
<% end %>
<%
if !#rows.nil?
#rows.each do |row|
end
%>
<%= row[:name] %>
<% end %>
what i basically want to achieve is this - based on the selected value from select tag
i want to display information(on the same view page) of selected item from the database
First of all you should write <%= form_tag not <% form_tag
Than, it's strange that your extract for select field id(options_from_collection_for_select(#projects,"id", "name"))
but in method show you search record by field lookup
You can use something of this sort to get this issue fixed
<%= check_box_tag "projectids[]",project.id %>
This would display the checkbox against each entry. And the checkbox is linked to the project object by its id. In the controller method, you will receive the project ids.
Then just directly use the submit tag to pass the parameters to the method.
I'll try and explain this as much and as easily as possible.
I have a Rails form, and 3 models.
Models: DemoModule, SalesDemo, and SalesDemoModule
What I want to do in my view/form is create a new SalesDemo, but a SalesDemo has many SalesDemoModules.
In the controller I have:
#sales_demo = SalesDemo.new
#demo_modules = DemoModule.find(:all, :conditions => ['active = true'])
How can I, in my view, have a text field row for each DemoModule, which I can pass back to the controller action, to save into SalesDemoModule?
You can specify that the SalesDemo accepts_nested_attributes_for SalesDemoModule, which then allows you to created a nested form (i.e. within a form_for a SalesDemo, you can have fields_for SalesDemoModule). Here's a simple example.
Simply put:
<%= form_for #sales_demo do |sales_demo_form| %>
<%= sales_demo_form.text_field "some_sales_demo_property" %>
<%= sales_demo_form.fields_for #demo_modules do |modules| %>
<%= modules.text_field "some_module_text_field" %>
<% end %>
<% end %>
In the SalesDemo, you will need to have
accepts_nested_attributes_for :demo_modules
You can get some more information here.