Manish Belsare
2010-Feb-28 10:36 UTC
How to access id passed to link_to function in controller?
<% if logged_in? and post.match?(post.id) %> <%= $n = post.id %> <%= link_to "Answer this Query!", :controller => "answers", :action => "new", :idd => post.id %> Hello sir, In the example given above, i am calling a new method to answer the query submitted by the user for the "idd" ie. according to the "id" of the question. Bt i cant get this id in the answer_controller which i want.. i hav even declared a global variable in the program which tracks the post id.. can u please help me in accessing the "idd" when the user clicks the link and goes to the action "new" page. I want the "particular id of the post that has been posted by the user as i am getting the array of post id''s but not the id of the particular post when i click it. Please Help.. Following is my code in answer controller.. $x = 0 class AnswersController < ApplicationController helper :profile, :avatar include ProfileHelper include AvatarHelper before_filter :protect, :load_post def new #flash[:notice] = post.id id=session[:user_id] Question.find_each do |@que| if(@que.user_id == id) # id is user id @que_id = "#{@que.id}" #que_id is question_id break end end idd=@que_id flash[:notice] = idd iddd=idd.to_i Post.find_each do |@pos| if(@pos.question_id == iddd) # id is question id => {iddd || @que_id.to_i} @pid = "#{@pos.id}" # pos_id is id in posts table #flash[:notice] = @pid.to_s break end end #flash[:notice] = id @posts = Post.find(@pid) @post = Post.find(params[:idd]) @answer = Answer.new @title = "Post your Answer Here!" #@post = @posts $x = @post.id mm = $x #flash[:notice] = mm @post = mm end def create @answer = Answer.new(params[:answer]) @answer.user = User.find(session[:user_id]) @answer.post = @post inc = 0 Answer.find_each do |@aid| inc = @aid.id.to_i #que_id is question_id end cnt = inc + 1 @answer.answer_id = cnt #flash[:notice] = "User ID is " + session[:user_id].to_s @answer.question_id = @que_id.to_i @post.answer_id = cnt respond_to do |format| if @answer.duplicate? or @post.answers << @answer flash[:notice] = ''Answer is Posted Successfully!.'' format.html { redirect_to post_url(:id => @post) } format.xml { head :created, :location => post_url(:id => @post) } else format.html { render :action => "new" } format.xml { render :xml => @post.errors.to_xml } end end end def destroy @answer = Answer.find(params[:id]) user = User.find(session[:user_id]) if @answer.authorized?(user) @answer.destroy else redirect_to hub_url return end respond_to do |format| format.html { redirect_to posts_url } format.xml { head :ok } end end private def load_post private def load_post id=session[:user_id] Question.find_each do |@que| if(@que.user_id == id) # id is user id @que_id = "#{@que.id}" #que_id is question_id break end end #flash[:notice] = @que_id @question = Question.find(@que_id) idd=@que_id #flash[:notice] = idd iddd=idd.to_i Post.find_each do |@pos| if(@pos.question_id == iddd) # id is question id => {iddd || @que_id.to_i} @pid = "#{@pos.id}" #pos_id is id in posts table #flash[:notice] = @pid.to_s break end end @post = Post.find(@pid) ................................EQN [1] end end Sir here as i hav marked here EQN[1] which suggests that i want the row in the posts table to be collected in instance variable @post. Bt the row should correspond to "idd => post.id" ie. @post should contain id of the post when i click the link. Also when i click i want to get the ques_string corresponding to the post.id i.e. I want @post.ques_string which it says "ques_string" is undefined? Waiting for ur Reply... -- Posted via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Colin Law
2010-Feb-28 10:56 UTC
Re: How to access id passed to link_to function in controller?
On 28 February 2010 10:36, Manish Belsare <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote:> <% if logged_in? and post.match?(post.id) %> > <%= $n = post.id %> > <%= link_to "Answer this Query!", > :controller => "answers", > :action => "new", > :idd => post.id %> > > Hello sir, > In the example given above, i am calling a new method to answer the > query submitted by the user for the "idd" ie. according to the "id" of > the question. > Bt i cant get this id in the answer_controller which i want..You have posted an apparently very complex set of questions with a lot of code. I think you might do better to try and simplify the problem as much as possible and ask just one question at a time. I will attempt to answer the simple question asked above. As you have coded it above then the value post.id should be available in params[:idd]. If it is not there then have a look in development.log and you will see the parameters sent from the browser. Colin -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Manish Belsare
2010-Feb-28 13:14 UTC
Re: How to access id passed to link_to function in controller?
Sir basically i want the idd from the link_to part i.e wen i click the "Answer this query!" link then the browser will redirect to: http://0.0.0.0:3000/answers/new?idd=9 now i want the idd = 9 to be assinged to the @post or even a query like @post = Post.find(params[:id]) will do.. bt i dont know how to get that id in answer controller.. i want the id which is 9 to be available in answer_controller... How 2 achieve this? -- Posted via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Colin Law
2010-Feb-28 13:39 UTC
Re: Re: How to access id passed to link_to function in controller?
On 28 February 2010 13:14, Manish Belsare <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote:> Sir basically i want the idd from the link_to part i.e wen i click the > "Answer this query!" link then the browser will redirect to: > http://0.0.0.0:3000/answers/new?idd=9The code you posted, that you have now snipped should do exactly that. As I suggested previously have a look in development.log and see what parameters are posted. In fact first look at the html of the page (View, Page source or similar in browser) and see what url has been generated for the link. Colin> > now i want the idd = 9 to be assinged to the @post > or even a query like @post = Post.find(params[:id]) will do.. > bt i dont know how to get that id in answer controller.. > > i want the id which is 9 to be available in answer_controller... > How 2 achieve this? > -- > Posted via http://www.ruby-forum.com/. > > -- > You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. > To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. > For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en. > >-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Manish Belsare
2010-Feb-28 13:40 UTC
Re: How to access id passed to link_to function in controller?
Sir even i want to know where does the id in the link : http://0.0.0.0:3000/answers/new?idd=9 is available.. Can i get this id in any variable whem i reach that page(action) so that i can access it and compare in a loop as i hav already got an array of id''s in instance variable.. So on comparing this id with id''s in the loop i can get the result...ie. @post -- Posted via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Manish Belsare
2010-Feb-28 13:47 UTC
Re: Re: How to access id passed to link_to function in controller?
<!DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html> <head> <title>Post your Answer Here!</title> <link href="/stylesheets/site.css?1267104655" media="screen" rel="stylesheet" type="text/css" /> <link href="/stylesheets/profile.css?1267104655" media="screen" rel="stylesheet" type="text/css" /> <script src="/javascripts/prototype.js?1267104655" type="text/javascript"></script> </head> <body> <div id="whole_page"> <div id="header">Knowledge Sharing Engine</div> <div id="nav"> <span style="float: right"> <a href="/user">Profile</a> | <a href="/user/logout">Logout</a> </span> <a href="/">Home</a> | <a href="/site/about">About Us</a> | <a href="/site/help">Help</a> | <a href="/community">Community</a> <hr noshade/> <div id="post_question"> <blink><a href="/user/register">Post A Question!</a></blink> </div> </div> <div id="content"> <h1> Post Your Answer</h1> <form action="/answers" method="post"> <fieldset> <legend>New Answer</legend> <div class="form_row"> <label for="ans_string">Answer String:</label> <textarea cols="60" id="answer_ans_string" name="answer[ans_string]" rows="20"></textarea> </div> <input class="submit" name="commit" type="submit" value="Create" /> </fieldset> </form> <br clear="all" /> </div> <div id="debug"> <a href="#" onclick="Element.toggle(''params_debug_info'');return false"> params</a> | <a href="#" onclick="Element.toggle(''session_debug_info'');return false"> session</a> <fieldset id="params_debug_info" class="debug_info" style="display:none"> <legend>params</legend> <pre class=''debug_dump''>--- !map:HashWithIndifferentAccess action: new controller: answers idd: "9" </pre> </fieldset> <fieldset id="session_debug_info" class="debug_info" style="display:none"> <legend>session</legend> <code class=''debug_dump''>{:user_id=>3, "flash"=>{}}</code> </fieldset> </div> <!--/div --> </body> </html> Sir this is the source generated.. Itz diff 2 understant da url generated.. n how to check the id in development.log file? i dnt hav any idea abt how 2 check it.. The simple contents of this file are: # Settings specified here will take precedence over those in config/environment.rb # In the development environment your application''s code is reloaded on # every request. This slows down response time but is perfect for development # since you don''t have to restart the webserver when you make code changes. config.cache_classes = false # Log error messages when you accidentally call methods on nil. config.whiny_nils = true # Show full error reports and disable caching config.action_controller.consider_all_requests_local = true config.action_view.debug_rjs = true config.action_controller.perform_caching = false config.action_mailer.delivery_method = :test # Raise Errors if the mailer can''t send config.action_mailer.raise_delivery_errors = true Please help -- Posted via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Colin Law
2010-Feb-28 13:48 UTC
Re: Re: How to access id passed to link_to function in controller?
On 28 February 2010 13:40, Manish Belsare <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote:> Sir even i want to know where does the id in the link : > http://0.0.0.0:3000/answers/new?idd=9 > is available.. > > Can i get this id in any variable whem i reach that page(action) so that > i can access it and compare in a loop as i hav already got an array of > id''s in instance variable.. > So on comparing this id with id''s in the loop i can get the result...ie. > @postThe idd value will be available in answers#new as params[:idd]. I suggest that you have a look at the rails guides at http://guides.rubyonrails.org/ Start with Getting Started, work through it making sure that you fully understand. Then move on to the other guides. Particularly ActiveRecord Associations and also Debugging. Then you should be able to answer basic questions yourself. Good luck. Colin -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Colin Law
2010-Feb-28 13:52 UTC
Re: Re: Re: How to access id passed to link_to function in controller?
On 28 February 2010 13:47, Manish Belsare <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote:>... > > Itz diff 2 understant da url generated.. > n how to check the id in development.log file? > i dnt hav any idea abt how 2 check it.. > The simple contents of this file are: > > # Settings specified here will take precedence over those in > config/environment.rbThat is not development.log that is development.rb The log files are in the folder call log As I said in previous email I think you need to start with some tutorials and guides in order to understand the basics of ruby and rails. Colin -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Manish Belsare
2010-Feb-28 14:02 UTC
Re: Re: How to access id passed to link_to function in controller?
Processing AnswersController#new (for 127.0.0.1 at 2010-02-28 19:21:26) [GET] Parameters: {"idd"=>"9"} [4;35;1mQuestion Load (0.5ms) [0m [0mSELECT * FROM `questions` WHERE (questions.id >= 0) ORDER BY questions.id ASC LIMIT 1000 [0m yes sir i get dat idd = 9 in the development.log file.. bt nw how to get it in the variable .. can i declare it as @post = idd? i need this answer urgently sir ..plzzz can u help me now? -- Posted via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Colin Law
2010-Feb-28 14:05 UTC
Re: Re: Re: How to access id passed to link_to function in controller?
On 28 February 2010 14:02, Manish Belsare <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote:> Processing AnswersController#new (for 127.0.0.1 at 2010-02-28 19:21:26) > [GET] > Parameters: {"idd"=>"9"} > [4;35;1mQuestion Load (0.5ms) [0m [0mSELECT * FROM `questions` > WHERE (questions.id >= 0) ORDER BY questions.id ASC LIMIT 1000 [0m > > > yes sir i get dat idd = 9 in the development.log file.. > bt nw how to get it in the variable .. > can i declare it as @post = idd?As I have said previously it will be available in params[:idd] in AnswersController#new Colin -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Manish Belsare
2010-Feb-28 14:10 UTC
Re: Re: Re: How to access id passed to link_to function in controller?
@pp = Post.find(params[:idd]) flash[:notice] = "Question: " + @pp.question_id sir when i do this it says cannot convert Fixnum into String.. Can u help me in resolving this problem? -- Posted via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Michael Pavling
2010-Feb-28 14:14 UTC
Re: Re: Re: Re: How to access id passed to link_to function in controller?
On 28 February 2010 14:10, Manish Belsare <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote:> @pp = Post.find(params[:idd]) > flash[:notice] = "Question: " + @pp.question_id > > > sir when i do this it says cannot convert Fixnum into String.. > Can u help me in resolving this problem?http://tinyurl.com/yh6bajd -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Manish Belsare
2010-Feb-28 14:30 UTC
Re: Re: Re: Re: How to access id passed to link_to function in controller?
before_filter :protect, :load_post private def load_post id=session[:user_id] Question.find_each do |@que| if(@que.user_id == id) # id is user id @que_id = "#{@que.id}" #que_id is question_id break end end #flash[:notice] = @que_id @question = Question.find(@que_id) idd=@que_id #flash[:notice] = idd iddd=idd.to_i Post.find_each do |@pos| if(@pos.question_id == iddd) # id is question id => {iddd || @que_id.to_i} @pid = "#{@pos.id}" #pos_id is id in posts table #flash[:notice] = @pid.to_s break end end @post = Post.find(@pid) @post = Post.find(params[:idd])...........(1) end Sir, even if i comment the entire lines in the prog and juzz keep eqn (1) which contains the "idd" and then execute the code, it says cannot find Post without an ID when @post = Post.find(params[:idd]) has that id of the post.. What could the reason be for the same? def create #@post = $n @answer = Answer.new(params[:answer]) @answer.user = User.find(session[:user_id]) @answer.post = @post inc = 0 Answer.find_each do |@aid| inc = @aid.id.to_i #que_id is question_id end cnt = inc + 1 @answer.answer_id = cnt #flash[:notice] = "User ID is " + session[:user_id].to_s @answer.question_id = @que_id.to_i @post.answer_id = cnt respond_to do |format| if @answer.duplicate? or @post.answers << @answer flash[:notice] = ''Answer is Posted Successfully!.'' format.html { redirect_to post_url(:id => @post) } format.xml { head :created, :location => post_url(:id => @post) } else format.html { render :action => "new" } format.xml { render :xml => @post.errors.to_xml } end end end This is the create action..where @post is required..Please help sir.. Thank yuh for ur worthy guidance! -- Posted via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Manish Belsare
2010-Feb-28 15:53 UTC
Re: Re: Re: Re: How to access id passed to link_to function in controller?
Sir, i want to know does only ''params[:idd]'' give the same id that was passed? Or i need to do Post.find([params[:idd]) and then contiune? -- Posted via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Punit Rathore
2010-Feb-28 18:58 UTC
Re: Re: Re: Re: How to access id passed to link_to function in controller?
params[:idd] would give you the "idd" you are passing. Post.find([params[:idd]) would return the post with id equal to the passed "idd". Try googling any issues. Most of the issues can be solved within 2 minutes. Manish Belsare wrote:> Sir, i want to know does only ''params[:idd]'' give the same id that was > passed? > Or i need to do Post.find([params[:idd]) and then contiune?-- Posted via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.