Basic rails question..I have the following: a user has many packs and a pack belongs to a category. In the new pack form I want make a select field with the pack's categories. I have the following code :
def new_pack
#pack=Pack.new()
#user= User.find(params[:id])
#categories = Category.all
end
def create
#pack=Pack.new(pack_params)
#user= User.find(params[:user_id])
if #pack.save
#user.packs << #pack
flash[:notice]="Thank you!"
redirect_to(:action=>'attempt_activation', :id=> #pack.id)
else
render :action=> "new_pack", :id=>#user.id
end
end
And in my view:
<%= form_for(:pack, :url=>{:controller=> "packs", :action=>'create', :user_id=> #user.id}) do |f| %>
<h5>Registration takes less than 2 minutes.</h5>
<label>Title<b style="color:red;">*</b>
<%= f.text_field(:title, :placeholder=>"") %>
</label>
<label>Category<b style="color:red;">*</b>
<%= f.select(:category_id, #categories.map {|s| [s.title, s.id]}, :selected=> #categories.second) %>
...
THE PROBLEM is that if there are errors at the form and and the new_pack action has to be rendered again it throws an error:
ActionView::Template::Error (undefined method `map' for nil:NilClass):
</div>
<div class="large-4 columns">
<label>Category
<%= f.select(:category_id, #categories.map {|s| [s.title, s.id]}, :selected=> #pack.category) %>
</div>
Why is this happening? render is used to render a particular view using the instance variables available in the action but here is not loading again the #categories.
Thank you.
Add #categories = Category.all in create method:
def create
#pack = Pack.new(pack_params)
#user = User.find(params[:user_id])
if #pack.save
#user.packs << #pack
flash[:notice] = "Thank you!"
redirect_to(:action => 'attempt_activation', :id => #pack.id)
else
#categories = Category.all
render :action=> "new_pack", :id => #user.id
end
end
Related
I'm editing a record and receive a constraint related error after hitting "Save". Then I correct the form field involved and click "Save". Ruby on Rails forces me to the show action whilst I'd rather stay on the edit form. Is this by design or I'm (hopefully) missing something here ?
controller
def update
#profile_field = safe_find ProfileField, params[:id], "profile field"
if #profile_field.update_attributes(input_params)
logger.tagged("#{self.class} #{__method__}") {logger.info #profile_field.inspect }
flash[:success] = "profile field updated"
redirect_to(request.referrer || root_path)
else
render 'edit'
end
end
view
= form_for #profile_field do |f|
= render 'shared/error_messages', object: f.object
.field
= f.label :name_en
= f.text_field :name_en
= f.label :name_et
= f.text_field :name_et
= f.label :name_ru
= f.text_field :name_ru
= f.label :field_type
= f.select :field_type, options_for_select(ProfileField.field_types,
:selected => f.object.field_type), :default => f.object.field_type
= button_tag(type: 'submit', class: 'btn btn-primary btn-block') do
%span.glyphicon.glyphicon-floppy-disk
Save
= link_to profile_fields_path, class: "btn btn-default btn-block" do
%span.glyphicon.glyphicon-arrow-left
Back
I'm getting ActiveModel::ForbiddenAttributesError in MicropostsController#create on line #2 in the create action.
Tried also changing f.hidden_field to hidden_field_tag but still getting ForbiddenAttributesError
micropost_controller
def create
tag = Tag.find(params[:micropost][:tag_id])
#micropost = tag.microposts.build(params[:micropost])
#micropost.user_id = current_user.id
if #micropost.save
flash[:success] = "Posted!"
redirect_to root_path
else
render 'static_pages/home'
end
end
tags_controller
def details
#tag = Tag.find(params[:id])
#microposts = #tag.microposts
#micropost = #tag.microposts.build if sign_in?
end
micropost form
<%= form_for(#micropost) do |f| %>
<%= render 'shared/error_messages', object: #micropost %>
<div class="field">
<%= f.text_area :content, placeholder: "Your post" %>
<%= f.hidden_field :tag_id %>
</div>
<%= f.submit "Post", class: "btn btn-large btn-primary" %>
<% end %>
in tags.rb
has_many :microposts, dependent: :destroy
in microposts.rb
belongs_to :user
belongs_to :tag
It seems that you need to delete tag_id from your microposts parameters:
tag = Tag.find(params[:micropost].delete(:tag_id))
#micropost = tag.microposts.build(params[:micropost])
If that won't fix it, just whit-list the params (it's good idea anyway):
micropost_params = params.require(:micropost).permit(:name, :title, ...)
#micropost = tag.microposts.build(micropost_params)
this
def create
tag = Tag.find(params[:micropost][:tag_id])
#...
end
should probably be changed to this
def create
tag = Tag.find(params[:tag_id])
#...
end
I am trying to show a form on the contact page, but it is from another controller.
The current code results in "First argument in form cannot contain nil or be empty"
After searching it looks there is a problem with the local hash that is not passed.
How can I correctly pass the locals with this code so that it works?
inquiries_controller.rb
class InquiriesController < ApplicationController
def new
#inquiry = Inquiry.new
end
def create
#inquiry = Inquiry.new(params[:inquiry])
if #inquiry.deliver
render :thank_you
else
render :new
end
end
end
inquiries_form.html.erb
<%= form_for #inquiry do |f|
f.text_field :name
f.text_field :email
f.text_area :message
f.submit "Send"
end %>
static_pages\contact.html.erb
<%= render "inquiries/form",
:inquiry => #inquiry %>
HI try adding this to your StaticPages Controller
class StaticPagesController < ApplicationController
def contact
#inquiry = Inquiry.new
end
end
Its a very common mistake. Also I believe your form may also be wrong unless you are using a gem that allows for that type of form. Let me know if this will fix your error.
Hello there i have been having problems trying to get my Name added onto my index.html via the new.html using form_for kindly look into the code below and correct my code please
My controller
def new
#post = Name.new
end
def create
#post = Name.new(post_params)
if #post.save
redirect_to names_path, notice: "your name was added"
else
render "new"
end
end
def post_param
params.require(:post).permit(:name)
end
My new.html.erb
<h1> Add your name </h1>
<%= form_for #post do |f| %>
<%= f.label :name %>
<%= f.text_field :name %>
<%= f.submit "Add your name" %>
<% end %>
I am having trouble in this lower part when i click the f.submit (the button that appears in the htlm) it throws me the error.
Your new and create methods should look like this
def new
#post = Post.new #here
end
def create
#post = Post.new(post_params) #here
if #post.save
redirect_to names_path, notice: "your name was added"
else
render "new"
end
end
You are confused of using attribute names with model name.You should be looking at these Guides.It should help you.
Update
As you said you created a Name model,you shouldn't be creating an attribute which is same as your model name.
Solution
Either change your attribute name or model name to some other meaningful name.
Update: Zishe figured it out. Correct params.require code should be:
def adventure_params
params.require(:adventure).permit(:story, :choice, :parent_id, :user_id)
end
with parenthesis instead of a bracket, of course.
Original question:
I am making a form_for that should be submitting 4 attributes to the Adventure model. The adventure model has ancestry. I keep getting wrong number of arguments (4 for 1) based on my params.require method. When I change to requirements down to 1, I see that all the attributes are blank in my database. I know they are in the params but for some reason they are not being saved. Here is my code:
Form
<div class="form">
<%= form_for #adventure do |f| %>
<%= f.label :choice %>
<%= f.text_area :choice %>
<%= f.label :story %>
<%= f.text_area :story %>
<%= f.label :parent_id %>
<%= f.text_field :parent_id %>
<%= f.submit "Post"%>
<% end %>
</div>
Controller
class AdventuresController < ApplicationController
before_filter :authenticate_user!
def home
end
def index
end
def new
#parent_id = params[:parent_id]
#adventure = Adventure.new
end
def show
#adventure = Adventure.find(params[:id])
end
def create
#user = current_user
#adventure = current_user.adventures.build(adventure_params)
if #adventure.save
flash[:success] = "Adventure created!"
redirect_to #adventure
else
flash[:error] = "There was an error"
redirect_to adventures_path
end
end
private
def adventure_params
params.require(:adventure).permit[:story, :choice, :parent_id, :user_id]
end
end
Model
class Adventure < ActiveRecord::Base
has_ancestry
belongs_to :user
validates :user_id, presence: true
validates :parent_id, presence: true
end
I have no idea why I am getting wrong number of arguments since the attributes show up in the params.
Change permit[...] to:
.permit(:story, :choice, :parent_id, :user_id)