jeudi 22 octobre 2015

Returning a selected value from collection in Rails and sending it to URL

So I have a database collection labeled @joe, and I am trying to pass the id variable into a link url path. This is in my index.erb.html file.

<%= select_tag "article", options_from_collection_for_select(@joe, 'id', 'title'), 
prompt: "Select Something" %>

In my articles_controller.rb file, I have

  def index
    @joe = Article.all
  end

I made a loop where it grabs each id number and passes it into the article_url path. I was wondering, in the dropdown box that I created above, when I select a certain value, I want to pass that value into the article.id variable. I'm having trouble with this and it outputs x amount of buttons.

<% @joe.each do |article| %>
    <%= button_to "SELECT", article_url(**parameter for article.id**), :method => 'get' %>
<% end %>

I'm trying to go to this articles_url link (i.e. localhost:3000/articles/1, localhost:3000/articles/2), where the numbers represent the id values from the elements of the collection @joe. I was wondering if it is possible to do this or if I can implement JavaScript or jQuery into this. Any help would be appreciated. Thank you!

And here's the rake routes command results:

      Prefix Verb   URI Pattern                  Controller#Action
  home_index GET    /home/index(.:format)        home#index
    articles GET    /articles(.:format)          articles#index
             POST   /articles(.:format)          articles#create
 new_article GET    /articles/new(.:format)      articles#new
edit_article GET    /articles/:id/edit(.:format) articles#edit
     article GET    /articles/:id(.:format)      articles#show
             PATCH  /articles/:id(.:format)      articles#update
             PUT    /articles/:id(.:format)      articles#update
             DELETE /articles/:id(.:format)      articles#destroy
        root GET    /                            home#index

Aucun commentaire:

Enregistrer un commentaire