ruby - Idiomatic Rails way to put a micropost's textbox and submit button, avoiding the "render 'form'" scaffold code? -
i've been trying ruby + rails today.
after creating standard user
, micropost
model, view , controllers, wanted add together page 1 could, beingness user, add together new micropost.
up moment, there's no authentication going on, i'd happy have page 1 user id via url, having thing in form text box 1 type out micropost add together site.
i've wired routing needed:
match 'users/new_micropost/:user_id' => 'users#new_micropost'
as controller's code:
def new_micropost @user = user.find(params[:user_id]) @micropost = micropost.new #still not sure end
i've re-create pasted microposts/new.html.erb
file new file named users/new_micropost.html.erb
:
<h1>new <%= @user.name %>'s micropost</h1> #added thingy <%= render 'form' %> <%= link_to 'back', users_path %>
what best way replace idiomatic way replace <%= render 'form' %>
print textbox asking me what's micropost want post on site plus submit button?
thanks
for form create own, passing in @micropost
variable, like:
<%= form_for @micropost |f| %> <%= f.text_area :body, :size => "60x12" %> <%= f.submit "post" %> <% end %>
have @ these guides more info. might need create separate create
action in controller action.
also, if want microposts associated users you'll need add together following:
class user < activerecord::base has_many :microposts, :dependent => :destroy end class micropost < activerecord::base belongs_to :user end
then alter new , create actions like:
def new_micropost @user = user.find(params[:user_id]) @micropost = @user.microposts.build end def create_micropost @user = user.find(params[:user_id]) @micropost = @user.microposts.build(params[:micropost]) if @micropost.save redirect_to some_path else render 'new_micropost' end end
click here read more associations.
note might have update form submit right controller: <%= form_for @micropost, url: create_micropost_path(@user.id) |f| %>
ruby-on-rails ruby ruby-on-rails-3
No comments:
Post a Comment