When I create a new comment the text saves fine but the commenter is not being saved. I have verified that the column for commenter exists and that the parameter is being passed. The field just isn't inserting into the table and I have no clue why.
Form:
<%= form_for #comment do |f| %>
<p>
<%= f.label :new_comment %>
<%= f.text_field :text %>
<%= hidden_field_tag :user_id, params[:user_id] %>
<%= hidden_field_tag :post_id, params[:id] %>
<%= f.submit "Comment" %>
</p>
<% end %>
Action:
def create
#user = User.find(params[:user_id])
#post = #user.posts.find(params[:post_id])
#comment = #post.comments.create(params[:comment].permit(:text, :commenter))
redirect_to show_post_path(#user, #post)
end
Add one more field in form which will pass commenter for comment
<%= form_for #comment do |f| %>
<p>
<%= f.label :new_comment %>
<%= f.text_field :text %>
<%= hidden_field_tag :user_id, params[:user_id] %>
<%= hidden_field_tag :post_id, params[:id] %>
<%= hidden_field_tag 'comment[commenter]', params[:user_id] %>
<%= f.submit "Comment" %>
</p>
<% end %>
OR
Change controller code to
def create
#user = User.find(params[:user_id])
#post = #user.posts.find(params[:post_id])
#comment = #post.comments.new(params[:comment].permit(:text, :commenter))
#comment.commenter = #user.id
#comment.save
redirect_to show_post_path(#user, #post)
end
I'm guessing you want your commenter_id in your form instead of user_id. Change your user_id line in your form to:
<%= hidden_field_tag :commenter_id, params[:commenter_id] %>
And then change the third line in your create function:
#comment = #post.comments.create(params[:comment].permit(:text, :commenter_id))
Have you check your association between User model and Comment Model?
in User model it should be has_many :comments and in Comment Model belongs_to :user
Related
I've problem with my "subscriber". It should just add an email to db with symptom active/inactive. It gaves me this error - undefined method `[]' for nil:NilClass
Extracted source (around line #2):
1: <div id="subscriber">
**2: <%= form_for #subscriber do |f| %>**
3: <%= f.label :email %>
4: <%= f.text_field :email %>
5: <%= f.hidden_field :active, :value => true %>
Here is model:
class Subscriber < ActiveRecord::Base
attr_accessor :email, :active
validates_format_of :email, :with => /\A([^#\s]+)#((?:[-a-z0-9]+\.)+[a-z]{2,})\Z/i
def initialize(attributes = {})
attributes.each do |name, value|
send("#{name}=", value)
end
end
def persisted?
false
end
end
This is my view:
<div id="subscriber">
<%= form_for #subscriber do |f| %>
<%= f.label :email %>
<%= f.text_field :email %>
<%= f.hidden_field :active, :value => true %>
<%= f.submit t('.submit') %>
<% end %>
</div>
Here is controller:
class SubscribersController < ApplicationController
def create
#subscriber = Subscriber.new(params[:subscriber])
if #subscriber.valid?
flash[:notice] = "Thx for subscription"
#subscriber.save
redirect_to root_url
else
render :action => 'new'
end
end
end
Here are routes:
resources :subscribers
I'm new with RoR, so if you need some more logs or info let me know. Thx alot.
I have a regular form for User information which starts like so:
<%= form_for(#user, :html => { :class => "form-horizontal" }) do |f| %>
<%= f.text_field :last_name %>
...
<%= f.submit "Submit", class: "btn btn-primary", :name => "submit_button" if #user.last_step? %>
<% end %>
I also have some non User fields in the form. Here's an example:
<%= f.label "When is your birthday?" %>
<%= select_tag "month" %>
<%= select_tag "day" %>
<%= select_tag "year" %>
How would I save this to a different table than User?
If you want to update a model other than User, you should probably use accepts_nested_attributes_for and fields_for. The alternative is to just do the work in your create action of your UsersController, ie:
def create
#user = User.create params[:user]
#foo = Foo.find params[:foo_id]
#foo.date = Date.new(params[:foo_year], params[:foo_month], params[:foo_day])
...
end
I have a form to create adverts.
Controllers:
def edit
#engines = Engine.all
#car = Car.find(params[:id])
end
def update
#car = Car.find(params[:id])
if #car.save
redirect_to root_path
end
end
My routes:
resources :adverts
Create.html.erb
<%= form_for #car, :url => adverts_path do |f| %>
<div><%= f.label :name %><br />
<%= f.text_field :name %></div>
<%= hidden_field_tag :model_id, params[:model_id] %>
<%= select_tag :engine_id, options_from_collection_for_select(#engines, "id", "name",:selected=>#car.engine_id) %>
<div><%= f.submit "Create car!" %></div>
<% end %>
I can create advert, but I can't to update it.
edit.html.erb
<%= form_for #car, :url => adverts_path do |f| %>
<div><%= f.label :name %><br />
<%= f.text_field :name %></div>
<%= hidden_field_tag :model_id, params[:model_id] %>
<%= select_tag :engine_id, options_from_collection_for_select(#engines, "id", "name",:selected=>#car.engine_id) %>
<div><%= f.submit "Update car!" %></div>
<% end %>
when I submited my form, I have an error - No route matches [PUT] "/adverts"
$ rake routes:
adverts GET /adverts(.:format) adverts#index
POST /adverts(.:format) adverts#create
new_advert GET /adverts/new(.:format) adverts#new
edit_advert GET /adverts/:id/edit(.:format) adverts#edit
advert GET /adverts/:id(.:format) adverts#show
PUT /adverts/:id(.:format) adverts#update
DELETE /adverts/:id(.:format) adverts#destroy
I need help.
When you are updating you have to let Rails know which object you want to update by passing an id.
In edit.html.erb change:
<%= form_for #car, :url => adverts_path do |f| %>
to:
<%= form_for #car, :url => advert_path(#car) do |f| %>
By the way, I find your code very strange. Why don't your model names match your controllers and routes? I mean you are creating an advert but your model is called car. That doesn't make any sense. Either call it car or advert, but don't mix them.
If you used RESTful routing, you don't need to specify a url, just need:
<%= form_for #car do |f| %>
The form can know #car is new record, or saved record, so it will send appropriate http method.
And in your update action:
def update
#car = Car.find(params[:id])
if #car.update_attributes(params[:car])
redirect_to root_path
end
end
I got myself in a similar situation today with a mismatched resource and model name. I agree the model and controller names need to correlate, but you can override the routes name to be whatever you want.
resources :cars, path: "adverts"
Along with RESTful routing
<%= form_for #car do |f| %>
You may also want to make sure your url: path is singular on the #form_form.
userhome_controller:
def edit_reviewer_email
#user = User.find(params[:id])
end
edit_reviewer_email:
<div class="pagetop center">
<%= form_for #user, update_reviewer_email_userhome_path(#user) do |f| %>
<%= f.label :email %><br />
<%= f.text_field :email, :size => 1 %>
<%= f.submit %>
<% end %>
</div>
error:
undefined method `stringify_keys' for "/userhome/18/update_reviewer_email":String
Should i be using form_tag when the model is accessed via a different controller?
I am looking for a rails 3 tutorial that covers a variety of forms if you know of any.
Thanks!
Your controller needs to limit its request to a single record (you can just use find):
#user = User.find(params[:id])
Or if you want to stick with a where statement:
#user = User.where("id = ?", params[:id]).first
I have a nested form (using accepts_nested_attributes_for in respective models):
<%= form_for(:technician, :url => {:controller => 'pos', :action => 'create_ticket'}) do |f| %>
<%= f.fields_for :service do |s| %>
<%= s.text_field :name %>
<%= s.text_field :name %>
<%= s.text_field :name %>
<%= s.text_field :name %>
<% end %>
<% end %>
This works fine if I only have one s.text_field. But once I add additional text_fields, it doesn't run properly. If you look at the source code, the id and name are the same for all six?
How do I put these params into an array? [so that I can isolate them like this:]
service1 = Service.named(params[:technician][:service][1][:name])
(I've tried the method described on railscasts episode 192, but it didn't work either).
After hours of trial and error, I've hacked something that works (but please let me know if you know a better, more eloquent way):
in view:
<%= form_for(:technician, :url => {:controller => 'pos', :action => 'create_ticket'}) do |f| %>
<% for #i in 1..6 do %>
<%= f.fields_for "services[#{#i}]" do |s| %>
<% end %>
<% end %>
<% end %>
in controller:
for i in 1..6 do
#service = Service.named(params[:technician][:services][i.to_s][:name]).first
end
make sure you turn i into string