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
[email protected]_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)

 [email protected]_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…

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

On 28 February 2010 10:36, Manish B. [email protected] wrote:

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

On 28 February 2010 13:14, Manish B. [email protected] 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=9

The 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

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?

Post your Answer Here!
<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/>
Post A Question!

Post Your Answer

New Answer

Answer String:

    <br clear="all" />
  </div>


  <div id="debug">
    <a href="#" onclick="Element.toggle('params_debug_info');return 

false">
params |

session


params

      <pre class='debug_dump'>--- !map:HashWithIndifferentAccess

action: new
controller: answers
idd: “9”



session
{:user_id=>3,
“flash”=>{}}

    </fieldset>
 </div>

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

On 28 February 2010 13:40, Manish B. [email protected] 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.
@post

The 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

On 28 February 2010 13:47, Manish B. [email protected] 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.rb

That 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

On 28 February 2010 14:02, Manish B. [email protected] 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

Processing AnswersController#new (for 127.0.0.1 at 2010-02-28 19:21:26)
[GET]
Parameters: {“idd”=>“9”}
e[4;35;1mQuestion Load (0.5ms)e[0m e[0mSELECT * FROM questions
WHERE (questions.id >= 0) ORDER BY questions.id ASC LIMIT 1000e[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?

@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?

On 28 February 2010 14:10, Manish B. [email protected] 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

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?

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 B. 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?

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)

 [email protected]_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!

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs