Why would link_to URL parameter not function properly with symbolAdd querystring parameters to link_toRoR custom routing/Method/View problem all methods come back as undefinedError while calling render_cell with argumentsRails NoMethodError in Classified#showModel has_many PaperclipModel. I have error “undefined method `img'”“undefined method `title' for nil:NilClass” Rails Guides TutorialHow to create array for params hashNoMethodError in Admin::Posts#indexI'm learning RoR through lynda.com and i need help solving this issueRoutes and def to_param “#self.id-#self.slug.parameterize”
Housemarks (superimposed & combined letters, heraldry)
Why are MBA programs closing in the United States?
Why did Intel abandon unified CPU cache?
Was Self-modifying-code possible just using BASIC?
Could a person damage a jet airliner - from the outside - with their bare hands?
Diatonic chords of a pentatonic vs blues scale?
Does putting salt first make it easier for attacker to bruteforce the hash?
How can I remove material from this wood beam?
Rail-to-rail op-amp only reaches 90% of VCC, works sometimes, not everytime
Confused with atmospheric pressure equals plastic balloon’s inner pressure
ASCII Meme Arrow Generator
I've been given a project I can't complete, what should I do?
Command of files and size
How durable are silver inlays on a blade?
Was planting UN flag on Moon ever discussed?
How to create a cubic equation that include sums of the roots of another cubic equation
Does the Nuka-Cola bottler actually generate nuka cola?
Multicolumn Output
How to avoid typing 'git' at the begining of every Git command
Why did the World Bank set the global poverty line at $1.90?
Is it okay to have a sequel start immediately after the end of the first book?
Analogy between an unknown in an argument, and a contradiction in the principle of explosion
The significance of kelvin as a unit of absolute temperature
How precise must a satellite's orbit be?
Why would link_to URL parameter not function properly with symbol
Add querystring parameters to link_toRoR custom routing/Method/View problem all methods come back as undefinedError while calling render_cell with argumentsRails NoMethodError in Classified#showModel has_many PaperclipModel. I have error “undefined method `img'”“undefined method `title' for nil:NilClass” Rails Guides TutorialHow to create array for params hashNoMethodError in Admin::Posts#indexI'm learning RoR through lynda.com and i need help solving this issueRoutes and def to_param “#self.id-#self.slug.parameterize”
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;
In a html.erb (called show.html.erb) that is displaying a post, I am adding a link_to in order to edit the post. There is only one line of change that I did
does not work
<%= link_to("edit", "/posts/:id/edit") %>
works
<%= link_to("edit", "/posts/#@post.id/edit") %>
For routes.rb, I have
get "posts/:id/edit => "posts#edit"
In the Controller,
def edit
@post = Post.find_by(id: params[:id])
end
in the edit.html.erb, I have
<textarea><%= @post.content %></textarea>
<input type="submit" value="save">
NoMethodError in Posts#edit
Showing /home/progate/tweet_app/app/views/posts/edit.html.erb where line #6 raised:
undefined method `content' for nil:NilClass
With my limited knowledge of Rails, I understand that because in the controller, I am defining @post with find_by, I would imagine that link URL can be using symbol :id.
But it seems like that is not the case.
Why does link_to with :id not work?
ruby-on-rails
add a comment |
In a html.erb (called show.html.erb) that is displaying a post, I am adding a link_to in order to edit the post. There is only one line of change that I did
does not work
<%= link_to("edit", "/posts/:id/edit") %>
works
<%= link_to("edit", "/posts/#@post.id/edit") %>
For routes.rb, I have
get "posts/:id/edit => "posts#edit"
In the Controller,
def edit
@post = Post.find_by(id: params[:id])
end
in the edit.html.erb, I have
<textarea><%= @post.content %></textarea>
<input type="submit" value="save">
NoMethodError in Posts#edit
Showing /home/progate/tweet_app/app/views/posts/edit.html.erb where line #6 raised:
undefined method `content' for nil:NilClass
With my limited knowledge of Rails, I understand that because in the controller, I am defining @post with find_by, I would imagine that link URL can be using symbol :id.
But it seems like that is not the case.
Why does link_to with :id not work?
ruby-on-rails
add a comment |
In a html.erb (called show.html.erb) that is displaying a post, I am adding a link_to in order to edit the post. There is only one line of change that I did
does not work
<%= link_to("edit", "/posts/:id/edit") %>
works
<%= link_to("edit", "/posts/#@post.id/edit") %>
For routes.rb, I have
get "posts/:id/edit => "posts#edit"
In the Controller,
def edit
@post = Post.find_by(id: params[:id])
end
in the edit.html.erb, I have
<textarea><%= @post.content %></textarea>
<input type="submit" value="save">
NoMethodError in Posts#edit
Showing /home/progate/tweet_app/app/views/posts/edit.html.erb where line #6 raised:
undefined method `content' for nil:NilClass
With my limited knowledge of Rails, I understand that because in the controller, I am defining @post with find_by, I would imagine that link URL can be using symbol :id.
But it seems like that is not the case.
Why does link_to with :id not work?
ruby-on-rails
In a html.erb (called show.html.erb) that is displaying a post, I am adding a link_to in order to edit the post. There is only one line of change that I did
does not work
<%= link_to("edit", "/posts/:id/edit") %>
works
<%= link_to("edit", "/posts/#@post.id/edit") %>
For routes.rb, I have
get "posts/:id/edit => "posts#edit"
In the Controller,
def edit
@post = Post.find_by(id: params[:id])
end
in the edit.html.erb, I have
<textarea><%= @post.content %></textarea>
<input type="submit" value="save">
NoMethodError in Posts#edit
Showing /home/progate/tweet_app/app/views/posts/edit.html.erb where line #6 raised:
undefined method `content' for nil:NilClass
With my limited knowledge of Rails, I understand that because in the controller, I am defining @post with find_by, I would imagine that link URL can be using symbol :id.
But it seems like that is not the case.
Why does link_to with :id not work?
ruby-on-rails
ruby-on-rails
asked Mar 24 at 21:08
Shintaro TakechiShintaro Takechi
369319
369319
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
Short Answer:
Because the first 'link_to' is literally taking you to 'www.yoursite.com/posts/:id/edit' while the second one uses something called "String Interpolation" to replace the # block with the ruby code inside of it. In this case, the proper Post ID taking you to 'www.yoursite.com/posts/1/edit' or whatever the id of the post is.
Long Answer:
When you created the route
get "posts/:id/edit => "posts#edit"
That ":id" is just a stand in for something else and becomes a parameter, so when you make a link_to using string literals ("/posts/:id/edit") it takes you to edit a post with an ID of ":id";
<%= link_to("edit", "/posts/:id/edit") %>
The second one works because it uses String Interpolation to inject the ID of the post into the link_to;
<%= link_to("edit", "/posts/#@post.id/edit") %>
becomes;
<%= link_to("edit", "/posts/1/edit") %>
and then finds a post with an ID of 1 and edits it.
The reason you are getting the error is because in the first one with no interpolation, it's not finding a Post, so post.content can't work since post is nil.
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55328606%2fwhy-would-link-to-url-parameter-not-function-properly-with-symbol%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
Short Answer:
Because the first 'link_to' is literally taking you to 'www.yoursite.com/posts/:id/edit' while the second one uses something called "String Interpolation" to replace the # block with the ruby code inside of it. In this case, the proper Post ID taking you to 'www.yoursite.com/posts/1/edit' or whatever the id of the post is.
Long Answer:
When you created the route
get "posts/:id/edit => "posts#edit"
That ":id" is just a stand in for something else and becomes a parameter, so when you make a link_to using string literals ("/posts/:id/edit") it takes you to edit a post with an ID of ":id";
<%= link_to("edit", "/posts/:id/edit") %>
The second one works because it uses String Interpolation to inject the ID of the post into the link_to;
<%= link_to("edit", "/posts/#@post.id/edit") %>
becomes;
<%= link_to("edit", "/posts/1/edit") %>
and then finds a post with an ID of 1 and edits it.
The reason you are getting the error is because in the first one with no interpolation, it's not finding a Post, so post.content can't work since post is nil.
add a comment |
Short Answer:
Because the first 'link_to' is literally taking you to 'www.yoursite.com/posts/:id/edit' while the second one uses something called "String Interpolation" to replace the # block with the ruby code inside of it. In this case, the proper Post ID taking you to 'www.yoursite.com/posts/1/edit' or whatever the id of the post is.
Long Answer:
When you created the route
get "posts/:id/edit => "posts#edit"
That ":id" is just a stand in for something else and becomes a parameter, so when you make a link_to using string literals ("/posts/:id/edit") it takes you to edit a post with an ID of ":id";
<%= link_to("edit", "/posts/:id/edit") %>
The second one works because it uses String Interpolation to inject the ID of the post into the link_to;
<%= link_to("edit", "/posts/#@post.id/edit") %>
becomes;
<%= link_to("edit", "/posts/1/edit") %>
and then finds a post with an ID of 1 and edits it.
The reason you are getting the error is because in the first one with no interpolation, it's not finding a Post, so post.content can't work since post is nil.
add a comment |
Short Answer:
Because the first 'link_to' is literally taking you to 'www.yoursite.com/posts/:id/edit' while the second one uses something called "String Interpolation" to replace the # block with the ruby code inside of it. In this case, the proper Post ID taking you to 'www.yoursite.com/posts/1/edit' or whatever the id of the post is.
Long Answer:
When you created the route
get "posts/:id/edit => "posts#edit"
That ":id" is just a stand in for something else and becomes a parameter, so when you make a link_to using string literals ("/posts/:id/edit") it takes you to edit a post with an ID of ":id";
<%= link_to("edit", "/posts/:id/edit") %>
The second one works because it uses String Interpolation to inject the ID of the post into the link_to;
<%= link_to("edit", "/posts/#@post.id/edit") %>
becomes;
<%= link_to("edit", "/posts/1/edit") %>
and then finds a post with an ID of 1 and edits it.
The reason you are getting the error is because in the first one with no interpolation, it's not finding a Post, so post.content can't work since post is nil.
Short Answer:
Because the first 'link_to' is literally taking you to 'www.yoursite.com/posts/:id/edit' while the second one uses something called "String Interpolation" to replace the # block with the ruby code inside of it. In this case, the proper Post ID taking you to 'www.yoursite.com/posts/1/edit' or whatever the id of the post is.
Long Answer:
When you created the route
get "posts/:id/edit => "posts#edit"
That ":id" is just a stand in for something else and becomes a parameter, so when you make a link_to using string literals ("/posts/:id/edit") it takes you to edit a post with an ID of ":id";
<%= link_to("edit", "/posts/:id/edit") %>
The second one works because it uses String Interpolation to inject the ID of the post into the link_to;
<%= link_to("edit", "/posts/#@post.id/edit") %>
becomes;
<%= link_to("edit", "/posts/1/edit") %>
and then finds a post with an ID of 1 and edits it.
The reason you are getting the error is because in the first one with no interpolation, it's not finding a Post, so post.content can't work since post is nil.
answered Mar 24 at 21:38
srngsrng
1667
1667
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55328606%2fwhy-would-link-to-url-parameter-not-function-properly-with-symbol%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown