我正在尝试创建一个带有2个操作按钮的基本应用程序:一个用于添加正面评分,一个用于添加负面评分。然而,当我尝试添加负分数时,它会添加正分数。我的代码如下所示。
控制器:
def profile
@user = User.find(params[:id])
@record = Record.where(user_id: @user.id)
@recordid = Record.find_by(user_id: @user.id)
end
def add_positive
@user = User.find(params[:id])
@recordid = Record.find_by(user_id: @user.id)
@recordid.positivescore +=1
if @recordid.save
redirect_back(fallback_location: records_path)
end
end
def add_negative
@user = User.find(params[:id])
@recordid = Record.find_by(user_id: @user.id)
@recordid.negativescore +=1
if @recordid.save
redirect_back(fallback_location: records_path)
end
end
我的观点:
<%= @record.each do |user_record| %>
Positive: <%= user_record.positivescore %> <%= button_to "Add Positive Score", :action => "add_positive" %>
Negative: <%= user_record.negativescore %> <%= button_to "Add Negative Score", :action => "add_negative" %>
<% end %>
我遇到的问题 - 每当我尝试添加负分数时,它会改为增加正分数。在我的头撞墙几个小时后,我发现这是因为我的routes.rb文件。
routes.rb中:
post '/profile/:id', to: 'users#add_positive'
post '/profile/:id', to: 'users#add_negative'
这让我意识到我可能错误地解决了整个问题,我不应该在我的控制器中定义3个单独的方法。有人可以指出我正确的方向,我应该这样做吗?
答案 0 :(得分:1)
您可以保留两种方法,但它们必须具有不同的路径:
import java.awt.BorderLayout;
import java.awt.CardLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
public class Test {
public static void main(String[] args) {
new Test();
}
public Test() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
ex.printStackTrace();
}
JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
CardLayout cardLayout = new CardLayout();
JPanel base = new JPanel(cardLayout);
base.add(makePanel(200, 200, Color.RED), "red");
base.add(makePanel(400, 400, Color.BLUE), "blue");
frame.add(base);
JButton blue = new JButton("Blue");
blue.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
cardLayout.show(base, "blue");
}
});
JButton red = new JButton("red");
red.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
cardLayout.show(base, "red");
}
});
JPanel buttons = new JPanel();
buttons.add(red);
buttons.add(blue);
frame.add(buttons, BorderLayout.SOUTH);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public TestPane makePanel(int width, int height, Color background) {
TestPane pane = new TestPane(new Dimension(width, height));
pane.setBackground(background);
return pane;
}
public class TestPane extends JPanel {
private JLabel label;
private Dimension preferredSize;
public TestPane(Dimension size) {
label = new JLabel("...");
setLayout(new GridBagLayout());
add(label);
this.preferredSize = size;
}
@Override
public Dimension getPreferredSize() {
return preferredSize;
}
@Override
public void invalidate() {
super.invalidate();
label.setText(getWidth() + "x" + getHeight());
}
}
}