Ruby-on-rails: passing parameters along a route and link_to

I have a name route:

map.up_vote 'up_vote', :controller => 'rep', :action => 'up_vote

But I up_voteneed to pass two arguments, postIDand posterID, and I can’t understand how to do this in partial, but in the integration test I have no problems.

Partial:

link_to 'Up Vote', up_vote_path, {:postID => session[:user_post_id], :postersID => session[:poster_id]} 

Integration Test:

post up_vote_path,{:postID => @latest.id,:postersID => users(:bob).id} (this works ok)

1) What happens in partial?

2) What changes can I make to my tests to catch this?

+3
source share
2 answers

Question: why do you pass your session variables to a link? You can get them directly from the session ...

, - : user_post_id : poster_id , :

1) URL-, ( , )

2) URL/.

( , , ):

:

map.resources :users do |user|
  user.resources :posts do |post|
    post.resource :vote
  end
end

, URL:

/users/:id/posts/:post_id/vote

:

link_to "Up", user_post_vote_path(@user, @post), :method => :create

@user @post , ,

link_to "Up", [@user, @post, :vote] # or [:vote, @post, @user]

:

class VoteController ....
  def create
    # do your stuff here
  end
end

, RESTful.

, .

+4

..._path

link_to "Up Vote", up_vote_path(:postID => session[:user_post_id], :postersID => session[:poster_id])

, link_to, .

post "" up_vote_path ", " {params}

, POST, : link_to

+3

Source: https://habr.com/ru/post/1727084/


All Articles