Does anyone know how to point the following comments form to the hello action in my comments controller below, any advice would be appreciated:
<h2>Add a comment:</h2>
<%= form_for([#venue, #venue.comments.build]) do |f| %>
<p>
<%= f.label :commenter %><br />
<%= f.text_field :commenter %>
</p>
<p>
<%= f.label :body %><br />
<%= f.text_area :body %>
</p>
<p>
<%= f.submit %>
</p>
<% end %>
//////////////////my comments controller:
def hello
#venue = Venue.find(params[:venue_id])
#comment = #venue.comments.create(params[:comment].permit(:commenter, :body))
redirect_to venue_path(#venue)
end
You have to set route properly, for example:
resources :venues do
resources :comments do
collection do
post :hello
end
end
end
and set form url to this action's url:
<%= form_for(#venue.comments.build, url: hello_venue_comments_path(#venue)) do |f| %>
Related
I am creating a form for nested models by following this http://railscasts.com/episodes/196-nested-model-form-part-1.
The example in the link is that Survey model has 3 questions. Each Question has 4 answers. I copy the example here but remove the Answer model to make it simpler.
So the surveys_controller has a new method that looks like this.
# surveys_controller.rb
def new
#survey = Survey.new
3.times do
question = #survey.questions.build
end
end
in views/surveys/_form.html.erb, we have this:
<%= form_for #survey do |f| %>
<%= f.error_messages %>
<p>
<%= f.label :name %><br />
<%= f.text_field :name %>
</p>
<%= f.fields_for :questions do |builder| %>
<%= render "question_fields", :f => builder %>
<% end %>
<p><%= f.submit "Submit" %></p>
<% end %>
and finally this is views/surveys/_question_fields.html.erb
<p>
<%= f.label :content, "Question" %><br />
<%= f.text_area :content, :rows => 3 %><br />
<%= f.check_box :_destroy %>
<%= f.label :_destroy, "Remove Question" %>
</p>
My question is if I set an attribute of each question in the aforementioned new method, how can I show that value in views.
Let's say I modify the surveys_controller.rb to this:
# surveys_controller.rb
def new
#survey = Survey.new
3.times do
question = #survey.questions.build
question.another_attribute = another_value # added line
end
end
How to show the value of question.another_attribute inside <%= f.fields_for :questions do |builder| %> or _question_fields.html.erb?
You are passing :f => builder into the partial, so you can work with f. Calling f.object should return the form's object, in this case a question. So the following should work:
<%= f.object.another_attribute %>
Error: ActionController::ParameterMissing in PostsController#new
param is missing or the value is empty: post
I'm working on a 2 model rails app and when I try to go to the "new post" link to create a post this error comes up.
BUT none of my actual code is highlighted...a blank line is highlighted above my "private, def post-params" code.
def destroy
#post = Post.find(params[:id])
#post.destroy
redirect_to posts_path
end
error code highlights here
private
def post_params
params.require(:post).permit(:name, :photo_url, :description, :resume)
end
form.html.erb
<%= form_for :post, url: posts_path(#post), method: :post do |f| %>
<p><%= f.label :name %><br> <%= f.text_field :name %> </p>
<p> <%= f.label :photo_url %><br> <%= f.text_area :photo_url %> </p>
<p> <%= f.label :description %><br> <%= f.text_area :description %></p>
<p> <%= f.label :resume %><br> <%= f.text_area :resume %> </p>
<p> <%= f.submit %> </p>
<% end %>
I have all of the correct params from my schema listed, so I think it might have to do with syntax or my path?
Nothing I've changed in syntax or path naming has worked so far.
I have been following the tutorial (http://guides.rubyonrails.org/getting_started.html ) to create my own web app for create a contact manager.
When I want to create a new contact, I have code in my contacts_controller
class ContactsController < ApplicationController
def show
#contact = Contact.find(params[:id])
end
def new
end
def create
#contact = Contact.new(contact_params)
#contact.save
redirect_to #contact
end
private
def contact_params
params.require(:contact).permit(:firstname, :lastname, :email, :phonenumber, :notes)
end
end
In the view I have
<h1>New Contact</h1>
<%= form_for :contact, url: contacts_path do |f| %>
<p>
<%= f.label :firstname %><br>
<%= f.text_field :firstname %>
</p>
<p>
<%= f.label :lastname %><br>
<%= f.text_field :lastname %>
</p>
<p>
<%= f.label :email %><br>
<%= f.text_field :email %>
</p>
<p>
<%= f.label :phonenumber %><br>
<%= f.text_field :phonenumber %>
</p>
<p>
<%= f.label :notes %><br>
<%= f.text_area :notes %>
</p>
<p>
<%= f.submit %>
</p>
<% end %>
In routes.rb
Rails.application.routes.draw do
get 'welcome/index'
root 'welcome#index'
resources :contacts
end
However, when I submit the form, I got this error message.
I think I followed the tutorial correctly and have the new method in my class. This seems weird to me and I have no idea to start debugging this app.
I can't find what I did wrong here, is there something I'm missing?
My new action/view here:
<h1>Submit a new experiment here!</h1>
<%= render "form" %>
My _form.html.erb form partial
<h1>THis is a form</h1>
<%= form_for(#experiment) do |f| %>
<div class="field">
<%= f.label :title %><br />
<%= f.text_field :title %>
</div>
<div class="field">
<%= f.label :description %><br />
<%= f.text_field :description %>
</div>
<div class="field">
<%= f.label :plan %><br />
<%= f.text_field :plan %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
My experiments controller:
class ExperimentController < ApplicationController
def index
end
def new
#experiment = Experiment.new
end
end
And my routes:
devise_for :users
resources :home
resources :experiment
root to: "home#index"
I am visiting the following url:
http://localhost:3000/experiment/new
In you want to follow rails convention then:
Rename your controller from ExperimentController to ExperimentsController, i.e plural Experiments.
Rename your controller file name app/controllers/experiment_controller.rb to app/controllers/experiments_controller.rb, i.e. plural experiments_controller.rb.
Update your routes by modifying resources :experiment to resources :experiments.
If not, update your form_for call as:
<%= form_for(#experiment, url: url_for(controller: 'experiment', action: 'create')) do |f| %>
...
<% end %>
I have the following simple rails page:
<h1> New User </h1>
<%= form_for :user, url:users_path do |f| %>
<p>
<%= f.label :email %>
<%= f.text_field :email %>
</p>
<p>
<%= f.label :password %>
<%= f.text_field :password %>
</p>
<%= f.submit %>
<% end %>
The create action gets executed and I want to access the :email attribute.
def create
render text: params[:email].inspect
end
The above always displays nil.
form_for :user will place all parameters beneath a :user key
render text: params[:user][:email].inspect