ruby - Rails form_for own action routes -
i want modify action (submit) form_for helper
<%= form_for(@rating, :as => :post, :url => demo_create_rating_path(@rating)) |f| %> <div class="field"> <%= f.label :value %><br /> <%= f.select :value, %w(1 2 3 4 5) %> </div> <%= f.hidden_field :article_id, :value => @article.id%> <%= f.hidden_field :user_id, :value => current_user.id %> <div class="field"> <%= f.label :description %><br /> <%= f.text_area :description, size: "100x5" %> </div> <div class="actions"> <%= f.submit %> </div> <% end %> this view , not work.
all want is, can redirekt action after submit button error:
actioncontroller::routingerror (no route matches {:controller=>"demo_ratings", :action=>"create", :article_id=>#<rating id: nil, value: nil, description: nil, article_id: nil, user_id: nil, created_at: nil, updated_at: nil>}): app/views/demo_ratings/_form.html.erb:1:in `_app_views_demo_ratings__form_html_erb__1912848844925280312_70155649546120' app/views/demo_ratings/new.html.erb:13:in `_app_views_demo_ratings_new_html_erb__27525029454473720_70155632487040' what doing wrong?
update
all funktion need form_for helper:
def new @rating = rating.new @article = article.find(params[:article_id]) end def edit @rating = rating.find(params[:id]) @article = article.find(params[:article_id]) end def create @rating = rating.new(params[:rating]) if @rating.save @article= article.find(params[:article_id]) puts @article.name puts @rating.id @rating.article = @article puts @rating.article.name redirect_to demo_rating_path(@rating, :article_id => @article.id), notice: 'rating created.' else render action: "new" end end def update @rating = rating.find(params[:id]) if @rating.update_attributes(params[:rating]) @article = @rating.article redirect_to demo_rating_path(@rating), notice: 'rating updated.' else render action: "edit" end end
try this:
<%= form_for(@rating, :as => :post, :url => demo_create_rating_path) |f| %> the @rating in url providing nil object id, , don't have id yet.
if want share form between create , update, use following:
<% form_for(@rating, :as => :post) |f| %> for reference, review output of rails generated scaffold's _form.html.erb.
in controller, saving new/ updated record before processing. statement if @rating.save should come after @rating.article = @article.
def create @rating = rating.new(params[:post]) @article= article.find(params[:article_id]) @rating.article_id = @article.id if @rating.save redirect_to demo_rating_path(@rating, :article_id => @article.id), notice: 'rating created.' else render action: "new" end end
Comments
Post a Comment