Why can I put params[:from] in the method parameter?
Hey Guys, I'm new to Ruby and Rails, I made a small test example:
/apps/helpers/
module HomeHelper
def makeach(from, to, by)
x = from
while x <= to
yield x
x = x + by
end
end
end
/apps/view/home/new.erb
<form action="create" method="post">
<input type="text" name="from" />
<input type="text" name="to" />
<input type="text" name="by" />
<input type="submit" name="commit" value="Click it" />
</form>
/apps/views/home/create.erb
module HomeHelper
<% makeach(params[:from], params[:to], params[:by]) do |x| %>
<%= x %>
<% end %>
/apps/controllers/home_controller.rb
class HomeController < ApplicationController
de开发者_StackOverflow中文版f index
end
def create
end
def new
end
end
After I post the form in action new
, the create view
outputs nothing in the browser
PS: I know there are a lot ways to improve my code, But here I just want to know why I got nothing.
So, one thing is you're missing is the basic idea of REST. For a solid explanation of how Rails handles control flow, try this answer.
But, basically, when you set resources :home
in your routes it defines seven routes for your seven standard controller actions ( SHOW, INDEX, NEW / CREATE, EDIT / UPDATE, DESTROY ).
When you try to load a page in your browser you're sending a GET request. The NEW action accepts GET requests. The CREATE action only accepts POST requests (like submitting a form). So, after you POST your form to the CREATE action, you need to redirect to a page that accepts GET requests if you want to show the visitor something.
Alternatively it may be possible to override Rails default to allow CREATE to accept GET requests, but I would consider that to be a bad idea.
Probably the simplest thing you need to do is make sure your CREATE action in your controller looks like this:
def create
(... do whatever processing of the params you want to do here ...)
redirect_to home_path # OR whatever other path you want.
end
At the end of a controller action you can either render (default) or redirect. Redirect just jumps to another controller action and does whatever is in that action. Render typically shows a view with the same name as a controller action, but render has lots of options, see here.
To access params in the view the best thing to do is to assign them to an instance variable. IE:
def ... # any action that accepts GET requests
@value = params[:value]
end
However, params will not persist after a redirect. So if you POST to one action and redirect to another (the normal convention), you'll need to either save the params to the DB, or save them to session.
In your case it looks like you should store them in session since they don't seem to be attached to a model.
Then in the next controller action you would check if there are certain variables in session and put them in instance variables to use in your view.
IE:
@value = session[:value] if session[:value]
I know this is a lot to absorb, but I hope this helps you wrap your mind around what's going on. One final suggestion, buy and read Beginning Rails 3. It's short, sweet, too the point, only takes a weekend to work through, and by the end you'll really understand the big picture of how rails works and your productivity and future learning will be quadrupled.
Good luck!
精彩评论