rails 3.1 select_tag usage - ruby-on-rails

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 %>

Related

Rails 5 custom method resource previous/next in controller or model?

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

Ruby on Rails: Including actions of different controllers into index view of another controller

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 %>

Rails search form in different controller?

Okay say I have a simple search form for Projects records like in the railscast found here: http://railscasts.com/episodes/37-simple-search-form?autoplay=true
Can you put the form_tag for the search in a different view other than the projects index.html? Like in a static_pages controller type of deal view? And how could you redirect to the projects index.html view after submitting the search in such a case? Thanks
Add below code wherever you want to add search functionality.
<% form_tag projects_path, :method => 'get' do %>
<p>
<%= text_field_tag :search, params[:search] %>
<%= submit_tag "Search", :name => nil %>
</p>
<% end %>
Changes needed in your controller only. Try render view instead of redirect and you are done.
def index
#projects = Project.search(params[:search])
render `projects/index`
end

Rails Controllers - Adding a Custom Action

I have an Article resource and have defined resourceful routes for it. I want to create a simple page that shows the articles of the current user. I am aware that it is possible to do so by adding another action, for example 'search' to articles controller which will contain the custom code that searches for articles that have the same user id. And for the routes:
resources :articles do
get 'search'
end
But I'm not sure if adding a custom action is a good idea in this case. I'm thinking I can still use the index action (which shows all articles) and pass some sort of parameter from the url so that it can distinguish if the user wants to see all articles or just his own. But I'm not sure exactly how this can be done. Any help would be great. Thanks!
You can use the query string to pass parameters. see here
So you can pass something like .../articles?user_id=2
In your controller, just change the behavior according to the user_id parameter.
you don't need to create a new action/view for it.
You can add a small form to filter all articles or only my articles, for example:
<%= form_tag articles_path, method: :get do %>
<%= radio_button_tag :search, "all", :checked => true %>
<%= label_tag :all %><br />
<%= radio_button_tag :search, "my" %>
<%= label_tag :my_articles %><br />
<%= submit_tag "filter", name: nil %>
<% end %>
than in your controller:
def index
if params[:search] == 'my'
#articles = current_user.articles
else
#articles = Article.all
end

Passing a parameter between views in Rails

I am trying to accomplish something like this:
I am creating a simple blog. I have set up categories for my blog.
I want that when my user goes to posts/index, he sees a list of all categories.
Example:
Text
Image
Upon clicking on a category, my user gets redirected to the posts/new page, where the category_id field will by transmitted through a hidden_field.
So my code right now is:
in posts/index
<% #categories.each do |c| %>
<%= link_to c.name, new_post_path(:category => c.id) %><br />
<% end %>
and in my posts/_form i'm trying to do something like this
<%= f.hidden_field :category_id, :value => params[:category_id] %>
which is not working though, because the html output is
No value is being passed.
What is the correct way to proceed here?
Thx!
At first glance it looks like a simple mistake mixing up the param names category and category_id.
Try this:
<% #categories.each do |c| %>
<%= link_to c.name, new_post_path(:category_id => c.id) %><br />
<% end %>
Also, from what i can understand in your code, it seems a post belongs to a category. In such case, you could nest routes from one in another, and paths for creating nested object would become accessible, such as new_category_post(#category).
The routing would look like that:
resources :categories do
resources :posts
end
You can read about this matter here: http://guides.rubyonrails.org/routing.html

Resources