I have this bit of code in my email.controller
user = @current_user story = @current_story recipient = story.user
It doesn't work because @current_story isn't defined. How can I find the current page to make this work?
I have this bit of code in my email.controller
user = @current_user story = @current_story recipient = story.user
It doesn't work because @current_story isn't defined. How can I find the current page to make this work?
You haven’t given anywhere enough detail for us to help you.
@current_story - what are you expecting this variable to contain? Saying “the current page” won’t help. Do you mean the current URL (@request.request_uri)? The current Page object (in which case you must have found the Page object using some code, assign that to @current_story. A variable from the session? @current_story = session[:current_story].
We need a lot more information to be able to help. @current_story is an instance variable, but there’s no information on what you’re expecting it to magically contain.
Cheers,
Andy
Andy Jeffries wrote:
I have this bit of code in my email.controller
user = @current_user story = @current_story recipient = story.user
It doesn't work because @current_story isn't defined. How can I find the current page to make this work?
You haven't given anywhere enough detail for us to help you.
@current_story - what are you expecting this variable to contain? Saying "the current page" won't help. Do you mean the current URL (@request.request_uri)? The current Page object (in which case you must have found the Page object using some code, assign that to @current_story. A variable from the session? @current_story = session[:current_story].
We need a lot more information to be able to help. @current_story is an instance variable, but there's no information on what you're expecting it to magically contain.
Cheers,
Andy
The email is to be sent from a page containing a story and comments. I want it to go to @story.user's email. If I use story= Story.find_by_id(2) it works but I want an expression to find the current story not one specified. Does that tell you enough? Do you want more files sent?
Attachments: http://www.ruby-forum.com/attachment/4823/email_controller.rb
Neil Bye wrote:
The email is to be sent from a page containing a story and comments. I want it to go to @story.user's email. If I use story= Story.find_by_id(2) it works but I want an expression to find the current story not one specified. Does that tell you enough? Do you want more files sent?
I assume you have a show page that contains the story and the comments.
How are you triggering the email from that form? A hyperlink I assume? If that's the case, just pass the current story id as a parameter on the link_to, which makes that id available in the controller for your Story.find(params[:id]) or something close to that.
Neil Bye wrote:
The email is to be sent from a page containing a story and comments. I want it to go to @story.user's email. If I use story= Story.find_by_id(2) it works but I want an expression to find the current story not one specified.
Ar Chron wrote:
, just pass the current story id as a parameter on the link_to, which makes that id available in the controller for your Story.find(params[:id]) or something close to that.
I now have <%= link_to "Email this user", :controller => "email", :action => "correspond", :id => @user.login, :story_id => @story.id %>
In the email_controller.rb I have
story = Story.find(params[:story_id])
Is that what you meant and why does it not work?
What is the value of params[:story_id]? Look in the log.
Colin
Colin Law wrote:
I now have <%= link_to "Email this user", :controller => "email", :action => "correspond", :id => @user.login, :story_id �=> @story.id %>
In the email_controller.rb I have
story = Story.find(params[:story_id])
Is that what you meant and why does it not work?
What is the value of params[:story_id]? Look in the log.
Colin
This is the same problem as I posted in the Can't find without id thread. You dont need to answer both
This is all I have in the log:
Processing EmailController#correspond (for 127.0.0.1 at 2010-06-30 19:32:58) [GET] e[4;36;1mUser Load (0.5ms)e[0m e[0;1mSELECT * FROM "users" WHERE ("users"."id" = 1) LIMIT 1e[0m
ActiveRecord::RecordNotFound (Couldn't find Story without an ID): app/controllers/email_controller.rb:23:in `correspond'
Rendered rescues/_trace (57.5ms) Rendered rescues/_request_and_response (0.4ms) Rendering rescues/layout (not_found)
I'll look at the debugging section
Colin Law wrote:
I now have <%= link_to "Email this user", :controller => "email", :action => "correspond", :id => @user.login, :story_id �=> @story.id %>
In the email_controller.rb I have
story = Story.find(params[:story_id])
Is that what you meant and why does it not work?
What is the value of params[:story_id]? Look in the log.
Colin
This is the same problem as I posted in the Can't find without id thread. You dont need to answer both
You should not ask the same question twice. So what is the value of @story.id? You could just display it on the page to find out if you do not know. The page with the link_to "Email this user" ... that is.
Colin
Colin Law wrote:
You should not ask the same question twice.
I wouldn't have but the problem was so similar.
So what is the value of
@story.id? You could just display it on the page
It depends which story is shown, that's my problem, it won't pass as a parameter or some reason.
Of course it depends which story is shown, it is the id of the individual story. If the value is nil then no parameter will be passed, if it is not nil then there should be a parameter. You can also look at the html of the page (view page source in browser) to check that the id is there in the href.
Add code to show the id on the page and check that it also appears in the href.
Colin
Colin Law wrote:
@story.id? �You could just display it on the page
You can
also look at the html of the page (view page source in browser) to check that the id is there in the href.
Add code to show the id on the page and check that it also appears in the href.
Colin
This is from show.html.erb
<%= @story.id %> <%= link_to "Email this user", :controller => "email", :action => "correspond" , :id => "@story_id" %>
This is view source
2 <a href="/email/correspond/@story_id">Email this user</a>
This is the error that results
Couldn't find Story with ID=@story_id
Where am I going wrong
Colin Law wrote:
@story.id? �You could just display it on the page
You can
also look at the html of the page (view page source in browser) to check that the id is there in the href.
Add code to show the id on the page and check that it also appears in the href.
Colin
This is from show.html.erb
<%= @story.id %> <%= link_to "Email this user", :controller => "email", :action => "correspond" , :id => "@story_id"
That is passing the string "@story_id" as the id, you want the value, so just :id => @story_id
%>
This is view source
2 <a href="/email/correspond/@story_id">Email this user</a>
This is the error that results
Couldn't find Story with ID=@story_id
You should have been able to work it out from this, it is getting the string @story_id instead of 2
Colin
You should have been able to work it out from this, it is getting the string @story_id instead of 2
Colin
How can I make it take 2 without using @story.id to find it. If I move to story 3 it has to send 3. How does it know to do so? Sorry I just can't get it.
I suggested that in the link_to it should be :id => @story_id which provides the value (2 in the case you showed), not :id => "@story_id" which provides a string "@story_id"
I think you need to go through some basic ruby tutorials if that does not make sense.
Colin
<%= link_to "Email this user", :controller => "email", :action => "correspond" , :id => @story_id %> Gives exactly the same result
Neil Bye wrote:
I suggested that in the link_to it should be :id => @story_id which provides the value (2 in the case you showed), not :id => "@story_id" which provides a string "@story_id"
I think you need to go through some basic ruby tutorials if that does not make sense.
Colin
<%= link_to "Email this user", :controller => "email", :action => "correspond" , :id => @story_id %> Gives exactly the same result
ok @story.id
Got it at last thanks